ProgressDialog的使用
ProgressDialog 继承自AlertDialog,AlertDialog继承自Dialog,实现DialogInterface接口。
ProgressDialog的创建方式有两种,一种是new Dialog ,一种是调用Dialog的静态方法Dialog.show()。
- final ProgressDialog dialog = new ProgressDialog(this);
- dialog.show();
- ProgressDialog dialog2 = ProgressDialog.show(this, "提示", "正在登陆中");
- ProgressDialog dialog3 = ProgressDialog
- .show(this, "提示", "正在登陆中", false);
- ProgressDialog dialog4 = ProgressDialog.show(this, "提示", "正在登陆中",
- false, true);
-
- ProgressDialog dialog5 = ProgressDialog.show(this, "提示", "正在登陆中", true,
- true, cancelListener);
方式五中需要一个cancelListener,代码如下;
- private OnCancelListener cancelListener = new OnCancelListener() {
-
- @Override
- public void onCancel(DialogInterface dialog) {
-
- Toast.makeText(MainActivity.this, "进度条被取消", Toast.LENGTH_LONG)
- .show();
-
- }
- };
ProgressDialog的样式有两种,一种是圆形不明确状态,一种是水平进度条状态
第一种方式:圆形进度条
- final ProgressDialog dialog = new ProgressDialog(this);
- dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
- dialog.setCancelable(true);
- dialog.setCanceledOnTouchOutside(false);
- dialog.setIcon(R.drawable.ic_launcher);
-
- dialog.setTitle("提示");
-
- dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
-
- @Override
- public void onDismiss(DialogInterface dialog) {
-
-
- }
- });
-
- dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
-
- @Override
- public boolean onKey(DialogInterface dialog, int keyCode,
- KeyEvent event) {
-
- return false;
- }
- });
-
- dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
-
- @Override
- public void onCancel(DialogInterface dialog) {
-
-
- }
- });
-
- dialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定",
- new DialogInterface.OnClickListener() {
-
- @Override
- public void onClick(DialogInterface dialog, int which) {
-
-
- }
- });
- dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消",
- new DialogInterface.OnClickListener() {
-
- @Override
- public void onClick(DialogInterface dialog, int which) {
-
-
- }
- });
- dialog.setButton(DialogInterface.BUTTON_NEUTRAL, "中立",
- new DialogInterface.OnClickListener() {
-
- @Override
- public void onClick(DialogInterface dialog, int which) {
-
-
- }
- });
- dialog.setMessage("这是一个圆形进度条");
- dialog.show();
- new Thread(new Runnable() {
-
- @Override
- public void run() {
-
- try {
- Thread.sleep(5000);
-
-
- dialog.cancel();
-
- } catch (InterruptedException e) {
-
- e.printStackTrace();
- }
-
- }
- }).start();
其中通过Thread.sleep(5000)模拟后台操作。
cancel和dismiss方法本质都是一样的,都是从屏幕中删除Dialog,唯一的区别是:调用cancel方法会回调DialogInterface.OnCancelListener如果注册的话,dismiss方法不会回掉。
第二种方式:水平进度条
- final ProgressDialog dialog = new ProgressDialog(this);
- dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
- dialog.setCancelable(true);
- dialog.setCanceledOnTouchOutside(false);
- dialog.setIcon(R.drawable.ic_launcher);
- dialog.setTitle("提示");
- dialog.setMax(100);
- dialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定",
- new DialogInterface.OnClickListener() {
-
- @Override
- public void onClick(DialogInterface dialog, int which) {
-
-
- }
- });
- dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消",
- new DialogInterface.OnClickListener() {
-
- @Override
- public void onClick(DialogInterface dialog, int which) {
-
-
- }
- });
- dialog.setButton(DialogInterface.BUTTON_NEUTRAL, "中立",
- new DialogInterface.OnClickListener() {
-
- @Override
- public void onClick(DialogInterface dialog, int which) {
-
-
- }
- });
- dialog.setMessage("这是一个水平进度条");
- dialog.show();
- new Thread(new Runnable() {
-
- @Override
- public void run() {
-
- int i = 0;
- while (i < 100) {
- try {
- Thread.sleep(200);
-
- dialog.incrementProgressBy(1);
-
- i++;
-
- } catch (Exception e) {
-
- }
- }
-
- dialog.dismiss();
-
- }
- }).start();
更多的是使用自定义的ProgressDialog,以满足不同显示的需要。