Dialog工具箱 – ProgressDialog

本文详细介绍了Android中ProgressDialog的使用方法,包括创建普通ProgressDialog及带进度条的ProgressDialog,并提供了代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在此前的几个教程中引出过一些Dialog的使用方法,可从来没有系统全面的介绍Android平台上所有Dialog家族成员的情况(其实官方文档有相当明确的说明:参考1、参考2)。对于大部分人可以直接根据官方文档获得有关Dialog的使用方法,AR给出的有关Dialog参考教程可以作为额外的补充。

在这里为大家介绍ProgressDialog的使用方法:


1) 创建一个普通ProgressDialog(不带有ProgressBar)所必须的几个参数

Context: 指定当前Dialog的Container
Title:对话框标题
Message:对话框主体所显示的信息
Indeterminate:不确定性属性,这个属性对于ProgressDailog默认的转轮模式没有实际意义,默认下设置为true,它仅仅对带有ProgressBar的Dialog有作用。
2)以下两个为可选参数

Cancelable:增加一个可以Cancel当前Dialog的按钮,强制退出。
CancelListner:当前Dialog强制取消之后将会被执行,通常用来清理未完成的任务。
快速创建一个ProgressDialog的方法(代码片段):

01 private ProgressDialog dialog;

02 private Handler handler = new Handler() {

03 public void handleMessage(Message msg)

04 {

05 dialog.dismiss();

06 }

07 };

08 private Thread checkUpdate = new Thread()

09 {

10 public void run() {

11 try {

12 sleep(3000);

13 } catch (InterruptedException e) {

14 // TODO Auto-generated catch block

15 e.printStackTrace();

16 }

17 handler.sendEmptyMessage(0);

18 }

19 };

20 @Override

21 public void onCreate(<A title=Bundle href="http://developer.android.com/reference/android/os/Bundle.html" target=_blank>Bundle</A> savedInstanceState) {

22 super.onCreate(savedInstanceState);

23 setContentView(R.layout.main);

24 final Button button = (Button) findViewById(R.id.Compute);

25 button.setOnClickListener(new View.OnClickListener()

26 {

27 public void onClick(View arg0) {

28 // TODO Auto-generated method stub

29 checkUpdate.stop();

30 dialog = ProgressDialog.show(ARTestDrawing2D.this,

31 "AndroidRes",

32 "I am thinking...",

33 true,true);

34 checkUpdate.start();

35 }

36 });

37 }

上边的例子中引用了Handler(official)作为multi-thread相互传递数据的载体,接下来为ProgressDialog实现一个可视化进度条(progressBar)的方法中依然需要调用Handler(Internal),借用这个例子大家也可以多了解Handler的用法。


利用一个可视化进度条取代第一个例子中比较抽象的旋转轮,让用户更加直观的了解当前任务的进度。初始化的方法如下:

1 dialog = new ProgressDialog(ARTestDrawing2D.this);

2 dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

3 dialog.setTitle("AndroidRes");

4 dialog.setMessage("I am thinking...");

看起来稍微比第一个例子复杂一些,每个参数都单独利用成员方法赋值。
其中SetProgressStyle用来设置显示模式,通常为STYLE_HORIZONTAL。下边通过一个完整的例子了解它的初始化和设置方法(例子来源于Android官网):

01 static final int PROGRESS_DIALOG = 0;

02 Button button;

03 ProgressThread checkUpdate;

04 ProgressDialog dialog;

05 /** Called when the <A title=activity href="http://www.androidres.com/index.php/2009/02/09/activity/">activity</A> is first created. */

06 public void onCreate(Bundle savedInstanceState) {

07 super.onCreate(savedInstanceState);

08 setContentView(R.layout.main);

09 // Setup the button that starts the progress dialog

10 button = (Button) findViewById(R.id.Compute);

11 button.setOnClickListener(new OnClickListener(){

12 public void onClick(View v) {

13 showDialog(PROGRESS_DIALOG);

14 }

15 });

16 }

17 protected Dialog onCreateDialog(int id) {

18 switch(id) {

19 case PROGRESS_DIALOG:

20 dialog = new ProgressDialog(ARTestDrawing2D.this);

21 dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

22 dialog.setTitle("AndroidRes");

23 dialog.setMessage("I am thinking...");

24 checkUpdate = new ProgressThread(handler);

25 checkUpdate.start();

26 return dialog;

27 default:

28 return null;

29 }

30 }

31 // Define the Handler that receives messages from the thread and update the progress

32 final Handler handler = new Handler() {

33 public void handleMessage(Message msg) {

34 int total = msg.getData().getInt("total");

35 dialog.setProgress(total);

36 if (total >= 100){

37 dismissDialog(PROGRESS_DIALOG);

38 checkUpdate.setState(ProgressThread.STATE_DONE);

39 total = 0;

40 }

41 }

42 };

43 /** Nested class that performs progress calculations (counting) */

44 private class ProgressThread extends Thread {

45 Handler mHandler;

46 final static int STATE_DONE = 0;

47 final static int STATE_RUNNING = 1;

48 int mState;

49 int total;

50 ProgressThread(Handler h) {

51 mHandler = h;

52 }

53 public void run() {

54 mState = STATE_RUNNING;

55 total = 0;

56 while (mState == STATE_RUNNING) {

57 try {

58 Thread.sleep(100);

59 } catch (InterruptedException e) {

60 Log.e("ERROR", "AndroidRes Thread Interrupted");

61 }

62 Message msg = mHandler.obtainMessage();

63 Bundle b = new Bundle();

64 b.putInt("total", total);

65 msg.setData(b);

66 mHandler.sendMessage(msg);

67 total++;

68 }

69 }

70 /* sets the current state for the thread,

71 * used to stop the thread */

72 public void setState(int state) {

73 mState = state;

74 }

75 }

例子理解起来相对比较容易,对两个比较主要的对象做个简单的说明:
checkUpdate: 从主线程中创建一个用于计算某项任务的线程,同时也是利用ProgessBar所追踪的对象,指示这个线程的任务进度。
handler: 利用这个Handler可以将线程中的数据实时反映到主线程中的ProgessBar,使其可以实时接收到最新进度数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值