ProgressDialog.show(Context context, CharSequence title, CharSequence message);
简单的传入参数有context , 标题 , 提示信息
另外还可以包含其他参数,见下图
progressDialog = new ProgressDialog(MainActivity.this);
水平样式
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
圆形样式
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
标题,提示信息,图标的设置
progressDialog.setTitle("标题");
progressDialog.setMessage("提示信息");
progressDialog.setIcon(R.mipmap.ic_launcher);
progressDialog.setCancelable(true); // 能够返回
progressDialog.setCanceledOnTouchOutside(true); // 点击外部返回
progressDialog.setButton(ProgressDialog.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
progressDialog.dismiss();
}
});
progressDialog.setButton(ProgressDialog.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
progressDialog.dismiss();
}
});
progressDialog.setButton(ProgressDialog.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
progressDialog.dismiss();
}
});
progressDialog.setButton(ProgressDialog.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
progressDialog.dismiss();
}
});
progressDialog.setButton(ProgressDialog.BUTTON_NEUTRAL, "中立", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
progressDialog.dismiss();
}
});
progressDialog.setButton(ProgressDialog.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
progressDialog.dismiss();
}
});
progressDialog.show();
意思是是否处于不确定状态,所谓的不确定状态大家应该都见过:
就是下载东西的时候正在连接服务器的时候显示的状态
progressDialog.setProgress();
progressDialog.setSecondaryProgress();
private Runnable runnable = new Runnable() {
@Override
public void run() {
int i = 0;
while (true) {
try {
Thread.sleep(200);
i++;
handler.sendEmptyMessage(i);
} catch (Exception e) {
}
}
}
};
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (progressDialog != null) {
progressDialog.setProgress(msg.what % 100);
progressDialog.setSecondaryProgress(msg.what % 100 < 100 ? msg.what % 100 + 20 : 100);
}
}
};
测试代码已分享到Github – ProgressDialogTest