Android 对话框 (二)ProgressDialog

本文介绍如何使用ProgressDialog显示进度动画或进度条,适用于不确定或已定义的任务进度显示。通过示例代码展示了ProgressDialog的基本配置方法及如何通过Handler机制更新进度。

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

[size=large][color=red][b](原文)
/**[/b][/color][/size][size=large][b][color=green]
Showing a progress bar[/color][/b][/size]
[color=green]Creating a ProgressDialog[/color]
ProgressDialog是AlertDialog的子类,可以显示进度动画:用旋转的环表示进度未定义的task;用进度条表示定义了进度的task。这个dialog也可以提供按钮,比如下载过程中的取消按钮。
打开一个进度dialog简单到只要调用ProgressDialog.show()就可以了。
比如:
ProgressDialog dialog = ProgressDialog.show(MyActivity.this, "",
"Loading. Please wait...", true);

[b][size=large]效果:[/size][/b]
[img]http://dl.iteye.com/upload/picture/pic/89084/ebccc6f5-54a7-3a65-a3df-a5a9788f4724.gif[/img]
第一个参数是程序的context, 第二个参数是tittle,第三个是message, 第三个参数表示这个progress时候是不清楚的(它只在创建进度条的时候有意义, which is discussed in the next section).

显示一个用动画表示进度的进度条:
1、用构造方法ProgressDialog(Context)初始一个ProgressDialog。
2、用setProgressStyle(int)设置style为STYLE_HORIZONTAL,并设置其他的属性,比如message等。
3、准备显示dialog的时候调用show()或者使用onCreateDialog(int)来返回这个ProgressDialog。
4、可以调用setProgress(int)传递目前完成的全部百分比或者vincrementProgressBy(int)传递增量值来增加进度条显示的进度。

比如:
ProgressDialog progressDialog;
progressDialog = new ProgressDialog(mContext);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMessage("Loading...");
progressDialog.setCancelable(false);

设置ProgressDialog很简单,创建ProgressDialog的多数代码多数是用来更新它的。你会发现需要使用Handler来创建新的线程来进行这项工作并把进度反映到Activity的UI上。

Example ProgressDialog with a second thread
This example uses a second thread to track the progress of a process (which actually just counts up to 100). The thread sends a Message back to the main Activity through a Handler each time progress is made. The main Activity then updates the ProgressDialog.

package com.example.progressdialog;

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.view.View.OnClickListener;
import android.widget.Button;

public class NotificationTest extends Activity {
static final int PROGRESS_DIALOG = 0;
Button button;
ProgressThread progressThread;
ProgressDialog progressDialog;

/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

// Setup the button that starts the progress dialog
button = (Button) findViewById(R.id.progressDialog);
button.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
showDialog(PROGRESS_DIALOG);
}
});
}

protected Dialog onCreateDialog(int id) {
switch(id) {
case PROGRESS_DIALOG:
progressDialog = new ProgressDialog(NotificationTest.this);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMessage("Loading...");
progressThread = new ProgressThread(handler);
progressThread.start();
return progressDialog;
default:
return null;
}
}

// Define the Handler that receives messages from the thread and update the progress
final Handler handler = new Handler() {
public void handleMessage(Message msg) {
int total = msg.getData().getInt("total");
progressDialog.setProgress(total);
if (total >= 100){
dismissDialog(PROGRESS_DIALOG);
progressThread.setState(ProgressThread.STATE_DONE);
}
}
};

/** Nested class that performs progress calculations (counting) */
private class ProgressThread extends Thread {
Handler mHandler;
final static int STATE_DONE = 0;
final static int STATE_RUNNING = 1;
int mState;
int total;

ProgressThread(Handler h) {
mHandler = h;
}

public void run() {
mState = STATE_RUNNING;
total = 0;
while (mState == STATE_RUNNING) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
Log.e("ERROR", "Thread Interrupted");
}
Message msg = mHandler.obtainMessage();
Bundle b = new Bundle();
b.putInt("total", total);
msg.setData(b);
mHandler.sendMessage(msg);
total++;
}
}

/* sets the current state for the thread,
* used to stop the thread */
public void setState(int state) {
mState = state;
}
}
}
[color=red][size=large][b]**/[/b][/size][/color]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值