public class ProgressDialogActivity extends Activity {
private Button btn_large_pd, btn_horizontal_pd;
// 声明进度条对话框
ProgressDialog mProgressDialog;
// 进度
int mCount = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_progressdialog);
btn_large_pd = (Button) findViewById(R.id.btn_large_pd);
btn_horizontal_pd = (Button) findViewById(R.id.btn_horizontal_pd);
// 设置按钮事件监听
btn_large_pd.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
// 创建进度条对话框对象
mProgressDialog = new ProgressDialog(
ProgressDialogActivity.this);
// 设置进度条风格,风格为圆形、旋转的
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
// 设置ProgressDialog标题
mProgressDialog.setTitle("圆形进度条对话框提示");
// 设置ProgressDialog提示信息
mProgressDialog.setMessage("这是一个圆形进度条对话框。");
// 设置ProgressDialog标题图标
mProgressDialog.setIcon(R.drawable.ic_launcher);
// 设置ProgressDialog的进度条是否不明确
mProgressDialog.setIndeterminate(false);
// 设置ProgressDialog是否可以按退回键取消
mProgressDialog.setCancelable(true);
// 设置ProgressDialog的一个Button
mProgressDialog.setButton("确定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int i) {
// 点击确定按钮取消对话框
dialog.cancel();
}
});
mProgressDialog.show();
}
});
btn_horizontal_pd.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
mCount = 0;
// 创建ProgressDialog对象
mProgressDialog = new ProgressDialog(
ProgressDialogActivity.this);
// 设置进度条风格,风格为长形
mProgressDialog
.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
// 设置ProgressDialog标题
mProgressDialog.setTitle("长形进度条对话框提示");
// 设置ProgressDialog的提示信息
mProgressDialog.setMessage("这是一个长形进度条对话框!");
// 设置ProgressDialog标题图标
mProgressDialog.setIcon(R.drawable.button31);
// 设置ProgressDialog进度条进度
mProgressDialog.setProgress(100);
// 设置ProgressDialog的进度条是否不明确
mProgressDialog.setIndeterminate(true);
// 设置ProgressDialog是否可以按退回按键取消
mProgressDialog.setCancelable(true);
// 让ProgressDialog显示
mProgressDialog.show();
new Thread() {
public void run() {
try {
while (mCount <= 100) {
// 由线程来控制进度
mProgressDialog.setProgress(mCount++);
Thread.sleep(1000);
}
mProgressDialog.cancel();
} catch (InterruptedException e) {
mProgressDialog.cancel();
}
}
}.start();
}
});
}
}