2013年3月15日----ProgressDialog与线程整合应用

       在Android里,我们通常是通过Progress Dialog来显示一个“加载中”对话框,这个类封装在Android.app.ProgressDialog里,但需要留意的是Android的Progerss Dialog必须要在后台程序运行完毕前,以dismiss()方法来关闭取得焦点的对话框,否则程序就会陷入无法终止的无穷循环中;又或者在线程里不可有任何更改Context或parent View的任何状态、文字输出等事件,因为线程里的Context与View并不属于parent,两者之间也没有关联。所以在下面的范例中,我们以线程(Thread)来模拟后台的运行,再通过线程运行完毕时,关闭这个加载中的动画对话框。

package com.example.test01;
 
import java.util.Calendar;
 
import android.app.Activity;
importandroid.app.ProgressDialog;
importandroid.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
importandroid.widget.Button;
importandroid.widget.TextView;
 
public class MainActivityextends Activity {
 
       Button button01;
       TextView textView01;
       ProgressDialog progressDialog;
      
       @Override
       protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
             
              button01 = (Button)findViewById(R.id.button01);
              textView01 = (TextView)findViewById(R.id.textView01);
             
              button01.setOnClickListener(new View.OnClickListener()
              {
                     public void onClick(View v) {
                            //显示Progress对话框
                            progressDialog =ProgressDialog.show(MainActivity.this, "正在努力加载...","稍等一会儿吧~" , true);
                            textView01.setText("正在加载");
                            progressDialog.setOnDismissListener(newDialogInterface.OnDismissListener() {
                                   public voidonDismiss(DialogInterface dialog) {}});
                            //开启新线程
                            new Thread()
                            {
                                   public void run()
                                   {
                                          try {
                                                 //这里写上要后台运行的代码段
                                                 //为了明显看见效果,以暂停3秒作为示范
                                                 sleep(3000);
                                          } catch (Exception e) {
                                                 e.printStackTrace();
                                          }
                                          finally
                                          {
                                                 //卸载所创建的ProgressDialog对象
                                                 progressDialog.dismiss();
                                          }
                                   };
                                  
                            }.start();
                     }
              });
             
       }
 
       @Override
       public boolean onCreateOptionsMenu(Menu menu) {
              // Inflate the menu; this adds items to the action barif it is present.
              getMenuInflater().inflate(R.menu.main, menu);
              return true;
       }
}

Android.app.ProgressDialog类中,有如下几种ProgressDialog构造的方法:

Static

ProgressDialog.show (Context context, CharSequence title, CharSequence message)

Static

ProgressDialog.show (Context context, CharSequence title, CharSequence message, boolean indeterminate)

Static

ProgressDialog.show (Context context, CharSequence title, CharSequence message, boolean indeterminate, boolean cancelable)

Static

ProgressDialog.show (Context context, CharSequence title, CharSequence message, boolean indeterminate, boolean cancelable, DialogInterface.OnCancelListener cancelListener)

       需要留意的是,第一个参数必须为目前运行Activity的Context,否则会报错,所以在本范例中使用的是MainActivity.this;而为了让跳出的ProgressDialog显示标题以及内容,所以用到了第二第三个参数,最后一个参数可传可不传。


 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值