原文网址;http://blog.csdn.net/jamesliulyc/article/details/6375598
首先在onCreateDialog方法里创建一个ProgressDialog,如下:
- //this表示该对话框是针对当前Activity的
- progressDialog = new ProgressDialog(this);
- //设置最大值为100
- progressDialog.setMax(100);
- //设置进度条风格STYLE_HORIZONTAL
- progressDialog.setProgressStyle(
- ProgressDialog.STYLE_HORIZONTAL);
- progressDialog.setTitle("进度条对话框");
接下来就是进度条的进度更新,因为对话框对象一直被当前Dialog保存着,所以onCreateDialog在下次调用对话框时不会在被调,所以
进度条更新操作不能在onCreateDialog里面,而是在onPrepareDialog里面进行。
首先要将进度条置为0
- progressDialog.incrementProgressBy(-progressDialog.getProgress());
increamentProgressBy源码中是调用了ProgressBar的setProgress(mProgress + diff); mProgress是当前进度值,这里的diff就是-progressDialog.getProgress(),最后结果为0,所以也可以这么用,效果和调用incrementProgressBy一样的,如下:
- progressDialog.onStart();
- progressDialog.setProgress(0);
值得一提的是,不能直接只用setProgress(0)来设置,这样的话,第一次弹出对话框效果可以和上面的一样,但是之后再弹出对话框的进度一直都是100。为什么这里不能直接用setProgress(0),查看源码可以知道,
- if (mHasStarted) {
- mProgress.setProgress(value);
- onProgressChanged();
- } else {
- mProgressVal = value;
- }
mHasStarted第一次调用的时候是true,当进度条达到100%时会调用onStop方法(此方法为protected,无法直接调用),将mHasStarted设置为false,也就是无法setProgress(value)了,只能通过onStart来将mHasStarted设置为true。
当点击“显示进度条对话框”按钮,这时候需要很耗时然后才显示对话框,而往往都会导致假死,如果超过5秒未响应将强制关闭,所以这里需要添加个多线程来做进一步处理。而进度条的增加效果是很快的,所以这这里需要让大家看到效果,得停几毫秒后再继续。
为了使主线程不受影响(处理当前Activity的线程),在点击按钮且耗时操作完后需要发送消息给主线程,主线程再做相应处理。线程之间的消息传递和异步处理是通过Handler来做处理的,相关代码如下:
- new Thread() {
- public void run() {
- for(int i=0; i<=100; i++) {
- handler.sendEmptyMessage(INCREASE);
- if(progressDialog.getProgress() >= 100) {
- break;
- }
- try {
- Thread.sleep(50);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }.start();
Handler的处理如下:(Handler具体说明网上很多,可以参考此处 http://my.unix-center.net/~Simon_fu/?p=652)
- handler = new Handler() {
- @Override
- public void handleMessage(Message msg) {
- switch(msg.what) {
- case INCREASE:
- progressDialog.incrementProgressBy(1);
- if(progressDialog.getProgress() >= 100) {
- // progressDialog.dismiss();
- }
- break;
- }
- super.handleMessage(msg);
- }
- };
解释就到这里,先看个效果图:
实现步骤:
第一步,一个按钮用来点击后弹出对话框的
res/layout/progress_dialog_layout.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content">
- <Button android:id="@+id/button"
- android:text="显示进度条对话框"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"/>
- </LinearLayout>
第二步,
src/com/dialog/activity/ProgressDialogActivity.java
- package com.dialog.activity;
- import android.app.Activity;
- import android.app.Dialog;
- import android.app.ProgressDialog;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.Message;
- import android.view.View;
- import android.widget.Button;
- public class ProgressDialogActivity extends Activity {
- private final int PROGRESS_DIALOG = 1;
- private final int INCREASE = 0;
- private ProgressDialog progressDialog = null;
- private Handler handler = null;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.progress_dialog_layout);
- Button button = (Button) findViewById(R.id.button);
- View.OnClickListener listener =
- new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- showDialog(PROGRESS_DIALOG);
- }
- };
- button.setOnClickListener(listener);
- handler = new Handler() {
- @Override
- public void handleMessage(Message msg) {
- switch(msg.what) {
- case INCREASE:
- progressDialog.incrementProgressBy(1);
- if(progressDialog.getProgress() >= 100) {
- // progressDialog.dismiss();
- }
- break;
- }
- super.handleMessage(msg);
- }
- };
- }
- @Override
- public Dialog onCreateDialog(int id) {
- switch(id) {
- case PROGRESS_DIALOG:
- //this表示该对话框是针对当前Activity的
- progressDialog = new ProgressDialog(this);
- //设置最大值为100
- progressDialog.setMax(100);
- //设置进度条风格STYLE_HORIZONTAL
- progressDialog.setProgressStyle(
- ProgressDialog.STYLE_HORIZONTAL);
- progressDialog.setTitle("进度条对话框");
- break;
- }
- return progressDialog;
- }
- @Override
- public void onPrepareDialog(int id, Dialog dialog) {
- switch(id) {
- case PROGRESS_DIALOG:
- //将进度条清0
- progressDialog.incrementProgressBy(-progressDialog.getProgress());
- new Thread() {
- public void run() {
- for(int i=0; i<=100; i++) {
- handler.sendEmptyMessage(INCREASE);
- if(progressDialog.getProgress() >= 100) {
- break;
- }
- try {
- Thread.sleep(50);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }.start();
- break;
- }
- }
- }