1、实例化ProgressDialog
//实例化ProgressDialog
final ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
2、ProgressDialog的基本设置
//设置提示信息
progressDialog.setMessage("正在加载");
//设置弹窗标题
progressDialog.setTitle("进度条");
//设置弹窗图标
progressDialog.setIcon(R.drawable.ic_audiotrack_light);
// 能够返回
progressDialog.setCancelable(true);
// 点击外部返回
progressDialog.setCanceledOnTouchOutside(true);
//设置进度条
progressDialog.setProgress(100);
//设置进度条是否明确
progressDialog.setIndeterminate(true);
//设置进度条样式
//ProgressDialog.STYLE_SPINNER 环形精度条
//ProgressDialog.STYLE_HORIZONTAL 水平样式的进度条
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
3、ProgressDialog按钮添加及响应实现
在ProgressDialog中添加Button,可用setButton(int, String, DialogInterface.OnClickListener);或setButton(int, String, Message);
setButton(int, String, Message);用于按键显示信息
setButton(int, String, DialogInterface.OnClickListener);用于按键进行操作
int:用于确定按键类型
DialogInterface.BUTTON_POSITIVE(确定)
DialogInterface.BUTTON_NEGATIVE(取消)
DialogInterface.BUTTON_NEUTRAL(一般)。
String:用于显示Button的文本
DialogInterface.OnClickListener:用于监听按键,确定相应动作。
progressDialog.setButton(ProgressDialog.BUTTON_NEGATIVE, "Button", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// progressDialog.dismiss();
Toast.makeText(MainActivity.this,"你点击了弹窗的按钮",Toast.LENGTH_LONG).show();
}
});
4、显示弹窗
progressDialog.show();