`

Android 对话框 (二)ProgressDialog

阅读更多
(原文)
/**

Showing a progress bar

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

效果:

第一个参数是程序的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;
}
}
}
**/
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics