ProgressDialog,顾名思义,就是一个进度对话框,常用于显示载入进度、下载进度等。合理使用ProgressDialog能增加用户体验,让用户知道现在程序所处的状态。 下面是两种用法,第一种适合复杂环境,可以自定义风格,添加按钮等,而第二种只能简单的显示一个只有标题和信息的ProgressDialog
package com.pocketdigi.ProgressDialog;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Main extends Activity {
/** Called when the activity is first created. */
ProgressDialog pd1, pd2;
Button b1, b2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b1 = (Button) findViewById(R.id.b1);
b2 = (Button) findViewById(R.id.b2);
b1.setOnClickListener(showPd1);
b2.setOnClickListener(showPd2);
}
OnClickListener showPd1 = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
pd1 = new ProgressDialog(Main.this);
pd1.setTitle("PD1标题");
pd1.setProgressStyle(ProgressDialog.STYLE_SPINNER);// 转圈风格
// 进度条风格为ProgressDialog.STYLE_HORIZONTAL,使用setMax,setProgress,incrementProgressBy方法设置进度
pd1.setMessage("PD1信息");
pd1.setButton("关闭", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
pd1.dismiss();
}
});
//可以使用setButton2,setButton3来添加更多按钮
pd1.setCancelable(false);//不可被返回键取消对话框
pd1.show();
}
};
OnClickListener showPd2 = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
pd2=ProgressDialog.show(Main.this,"PD2标题","PD2信息");
//使用该方法将不能再设置按钮,适合显示简单的ProgressDialog
}
};
}