android dialog ——ProgressDialog 进度条对话框详解

 
首先在onCreateDialog方法里创建一个ProgressDialog,如下:
  1. //this表示该对话框是针对当前Activity的
  2. progressDialog = new ProgressDialog(this);
  3. //设置最大值为100
  4. progressDialog.setMax(100);
  5. //设置进度条风格STYLE_HORIZONTAL
  6. progressDialog.setProgressStyle(
  7.                 ProgressDialog.STYLE_HORIZONTAL);
  8. progressDialog.setTitle("进度条对话框");
复制代码



接下来就是进度条的进度更新,因为对话框对象一直被当前Dialog保存着,所以onCreateDialog在下次调用对话框时不会在被调,所以
进度条更新操作不能在onCreateDialog里面,而是在onPrepareDialog里面进行。
首先要将进度条置为0
  1. progressDialog.incrementProgressBy(-progressDialog.getProgress());
复制代码

increamentProgressBy源码中是调用了ProgressBar的setProgress(mProgress + diff); mProgress是当前进度值,这里的diff就是-progressDialog.getProgress(),最后结果为0,所以也可以这么用,效果和调用incrementProgressBy一样的,如下:
  1. progressDialog.onStart();
  2. progressDialog.setProgress(0);
复制代码

值得一提的是,不能直接只用setProgress(0)来设置,这样的话,第一次弹出对话框效果可以和上面的一样,但是之后再弹出对话框的进度一直都是100。为什么这里不能直接用setProgress(0),查看源码可以知道,
  1. if (mHasStarted) {
  2.       mProgress.setProgress(value);
  3.       onProgressChanged();
  4. } else {
  5.       mProgressVal = value;
  6. }
复制代码
mHasStarted第一次调用的时候是true,当进度条达到100%时会调用onStop方法(此方法为protected,无法直接调用),将mHasStarted设置为false,也就是无法setProgress(value)了,只能通过onStart来将mHasStarted设置为true。
当点击“显示进度条对话框”按钮,这时候需要很耗时然后才显示对话框,而往往都会导致假死,如果超过5秒未响应将强制关闭,所以这里需要添加个多线程来做进一步处理。而进度条的增加效果是很快的,所以这这里需要让大家看到效果,得停几毫秒后再继续。
为了使主线程不受影响(处理当前Activity的线程),在点击按钮且耗时操作完后需要发送消息给主线程,主线程再做相应处理。线程之间的消息传递和异步处理是通过Handler来做处理的,相关代码如下:
  1. new Thread() {
  2.         public void run() {
  3.                 for(int i=0; i<=100; i++) {
  4.                         handler.sendEmptyMessage(INCREASE);
  5.                         if(progressDialog.getProgress() >= 100) {
  6.                                 break;
  7.                         }
  8.                         try {
  9.                                 Thread.sleep(50);
  10.                         } catch (InterruptedException e) {
  11.                                 e.printStackTrace();
  12.                         }
  13.                 }
  14.         }
  15. }.start();
复制代码

Handler的处理如下:(Handler具体说明网上很多,可以参考此处http://my.unix-center.net/~Simon_fu/?p=652
  1. handler = new Handler() {
  2.         @Override
  3.         public void handleMessage(Message msg) {
  4.                 switch(msg.what) {
  5.                         case INCREASE:
  6.                                 progressDialog.incrementProgressBy(1);
  7.                                 if(progressDialog.getProgress() >= 100) {
  8.                                 //        progressDialog.dismiss();
  9.                                 }
  10.                                 break;
  11.                 }
  12.                 super.handleMessage(msg);
  13.         }
  14. };
复制代码




解释就到这里,先看个效果图:
1.gif
2011-6-16 09:14 上传
下载附件(23.27 KB)
实现步骤:
第一步,一个按钮用来点击后弹出对话框的
res/layout/progress_dialog_layout.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3.   xmlns:android="http://schemas.android.com/apk/res/android"
  4.   android:layout_width="fill_parent"
  5.   android:layout_height="wrap_content">
  6.   <Button android:id="@+id/button"
  7.     android:text="显示进度条对话框"
  8.           android:layout_width="fill_parent"
  9.           android:layout_height="wrap_content"/>
  10. </LinearLayout>
复制代码

第二步,
src/com/dialog/activity/ProgressDialogActivity.java
  1. package com.dialog.activity;

  2. import android.app.Activity;
  3. import android.app.Dialog;
  4. import android.app.ProgressDialog;
  5. import android.os.Bundle;
  6. import android.os.Handler;
  7. import android.os.Message;
  8. import android.view.View;
  9. import android.widget.Button;

  10. public class ProgressDialogActivity extends Activity {
  11.        
  12.         private final int PROGRESS_DIALOG = 1;
  13.        
  14.         private final int INCREASE = 0;
  15.        
  16.         private ProgressDialog progressDialog = null;
  17.        
  18.         private Handler handler = null;
  19.        
  20.     @Override
  21.     public void onCreate(Bundle savedInstanceState) {
  22.         super.onCreate(savedInstanceState);
  23.         setContentView(R.layout.progress_dialog_layout);
  24.        
  25.         Button button = (Button) findViewById(R.id.button);
  26.         View.OnClickListener listener =
  27.                 new View.OnClickListener() {
  28.                                
  29.                                 @Override
  30.                                 public void onClick(View view) {
  31.                                         showDialog(PROGRESS_DIALOG);
  32.                                 }
  33.                         };
  34.                 button.setOnClickListener(listener);
  35.                
  36.                 handler = new Handler() {
  37.                         @Override
  38.                         public void handleMessage(Message msg) {
  39.                                 switch(msg.what) {
  40.                                         case INCREASE:
  41.                                                 progressDialog.incrementProgressBy(1);
  42.                                                 if(progressDialog.getProgress() >= 100) {
  43.                                                 //        progressDialog.dismiss();
  44.                                                 }
  45.                                                 break;
  46.                                 }
  47.                                 super.handleMessage(msg);
  48.                         }
  49.                 };
  50.     }
  51.    
  52.     @Override
  53.     public Dialog onCreateDialog(int id) {
  54.             switch(id) {
  55.                     case PROGRESS_DIALOG:       
  56.                                 //this表示该对话框是针对当前Activity的
  57.                                 progressDialog = new ProgressDialog(this);
  58.                                 //设置最大值为100
  59.                                 progressDialog.setMax(100);
  60.                                 //设置进度条风格STYLE_HORIZONTAL
  61.                                 progressDialog.setProgressStyle(
  62.                                                 ProgressDialog.STYLE_HORIZONTAL);
  63.                                 progressDialog.setTitle("进度条对话框");
  64.                             break;
  65.             }
  66.             return progressDialog;
  67.     }
  68.    
  69.         @Override
  70.     public void onPrepareDialog(int id, Dialog dialog) {
  71.            
  72.             switch(id) {
  73.                     case PROGRESS_DIALOG:
  74.                             //将进度条清0
  75.                             progressDialog.incrementProgressBy(-progressDialog.getProgress());
  76.                             new Thread() {
  77.                                     public void run() {
  78.                                             for(int i=0; i<=100; i++) {
  79.                                                     handler.sendEmptyMessage(INCREASE);
  80.                                                         if(progressDialog.getProgress() >= 100) {
  81.                                                                 break;
  82.                                                         }
  83.                                                         try {
  84.                                                                 Thread.sleep(50);
  85.                                                         } catch (InterruptedException e) {
  86.                                                                 e.printStackTrace();
  87.                                                         }
  88.                                             }
  89.                                     }
  90.                             }.start();
  91.                             break;
  92.             }
  93.     }
  94. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值