Android用户界面开发(22):ProgressDialog

对话框中的进度条(ProgressDialog)

其实就是将进度条加入到对话框中去。

首先我们需要创建ProgressDialog对象,以下方法可以用来设置ProgressDialog:

setProgressStyle:设置进度条风格

setTitle:设置ProgressDialog标题

setMessage:设置ProgressDialog提示信息

setIcon:设置ProgressDialog标题图标

setIndeterminate:设置ProgressDialog的进度条是否明确可用

setCancelable:设置ProgressDialog是否可以按退回键取消

setButton:设置ProgressDialog的一个Button

show:显示ProgressDialog

m_pDialog = new ProgressDialog(Activity01.this);               

m_pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);// 设置进度条风格,风格为圆形,旋转的                            

m_pDialog.setTitle("提示");// 设置ProgressDialog 标题               

m_pDialog.setMessage("这是一个圆形进度条对话框");// 设置ProgressDialog 提示信息        

m_pDialog.setIcon(R.drawable.img1);// 设置ProgressDialog 标题图标                

m_pDialog.setIndeterminate(false);// 设置ProgressDialog 的进度条是否不明确

m_pDialog.setCancelable(true);// 设置ProgressDialog 是否可以按退回按键取消           

m_pDialog.setButton("确定", new DialogInterface.OnClickListener() {// 设置ProgressDialog 的一个Button

    public void onClick(DialogInterface dialog, int i)                  

    {                            

        dialog.cancel();//点击“确定按钮”取消对话框                

    }              

});                               

m_pDialog.show();// 让ProgressDialog显示

实现步骤:
第一步:一个按钮用来点击后弹出对话框的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

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:
progressDialog.incrementProgressBy(-progressDialog.getProgress()); //将进度条清0
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;
}
}
}

 

效果图:

Android用户界面开发(22):ProgressDialog

 

官网参考链接:http://developer.android.com/reference/android/app/ProgressDialog.html

参考链接:http://www.apkbus.com/forum.php?mod=viewthread&tid=4131

源码下载链接: http://www.apkbus.com/android-117160-1-1.html

你可能感兴趣的:(ProgressDialog)