以前对ProgressDialog不太熟练,特地看到的这个文章

ProgressDialog的使用 

ProgressDialog 继承自AlertDialog,AlertDialog继承自Dialog,实现DialogInterface接口。

ProgressDialog的创建方式有两种,一种是new Dialog ,一种是调用Dialog的静态方法Dialog.show()。

  1. // 方式一:new Dialog
  2. final ProgressDialog dialog = new ProgressDialog(this);
  3. dialog.show();
	// 方式一:new Dialog
final ProgressDialog dialog = new ProgressDialog(this);
dialog.show();
  1. // 方式二:使用静态方式创建并显示,这种进度条只能是圆形条,设置title和Message提示内容
  2. ProgressDialog dialog2 = ProgressDialog.show(this, "提示", "正在登陆中");
	// 方式二:使用静态方式创建并显示,这种进度条只能是圆形条,设置title和Message提示内容
ProgressDialog dialog2 = ProgressDialog.show(this, "提示", "正在登陆中");
  1. // 方式三 使用静态方式创建并显示,这种进度条只能是圆形条,这里最后一个参数boolean indeterminate设置是否是不明确的状态
  2. ProgressDialog dialog3 = ProgressDialog
  3. .show(this, "提示", "正在登陆中", false);
	// 方式三 使用静态方式创建并显示,这种进度条只能是圆形条,这里最后一个参数boolean indeterminate设置是否是不明确的状态
ProgressDialog dialog3 = ProgressDialog
.show(this, "提示", "正在登陆中", false);
  1. // 方式四 使用静态方式创建并显示,这种进度条只能是圆形条,这里最后一个参数boolean cancelable 设置是否进度条是可以取消的
  2. ProgressDialog dialog4 = ProgressDialog.show(this, "提示", "正在登陆中",
  3. false, true);
	// 方式四 使用静态方式创建并显示,这种进度条只能是圆形条,这里最后一个参数boolean cancelable 设置是否进度条是可以取消的
ProgressDialog dialog4 = ProgressDialog.show(this, "提示", "正在登陆中",
false, true);
  1. // 方式五 使用静态方式创建并显示,这种进度条只能是圆形条,这里最后一个参数 DialogInterface.OnCancelListener
  2. // cancelListener用于监听进度条被取消
  3. ProgressDialog dialog5 = ProgressDialog.show(this, "提示", "正在登陆中", true,
  4. true, cancelListener);
	// 方式五 使用静态方式创建并显示,这种进度条只能是圆形条,这里最后一个参数 DialogInterface.OnCancelListener
// cancelListener用于监听进度条被取消
ProgressDialog dialog5 = ProgressDialog.show(this, "提示", "正在登陆中", true,
true, cancelListener);

方式五中需要一个cancelListener,代码如下;

  1. private OnCancelListener cancelListener = new OnCancelListener() {
  2. @Override
  3. public void onCancel(DialogInterface dialog) {
  4. // TODO Auto-generated method stub
  5. Toast.makeText(MainActivity.this, "进度条被取消", Toast.LENGTH_LONG)
  6. .show();
  7. }
  8. };
	private OnCancelListener cancelListener = new OnCancelListener() {

		@Override
public void onCancel(DialogInterface dialog) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "进度条被取消", Toast.LENGTH_LONG)
.show(); }
};

ProgressDialog的样式有两种,一种是圆形不明确状态,一种是水平进度条状态

第一种方式:圆形进度条

  1. final ProgressDialog dialog = new ProgressDialog(this);
  2. dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);// 设置进度条的形式为圆形转动的进度条
  3. dialog.setCancelable(true);// 设置是否可以通过点击Back键取消
  4. dialog.setCanceledOnTouchOutside(false);// 设置在点击Dialog外是否取消Dialog进度条
  5. dialog.setIcon(R.drawable.ic_launcher);//
  6. // 设置提示的title的图标,默认是没有的,如果没有设置title的话只设置Icon是不会显示图标的
  7. dialog.setTitle("提示");
  8. // dismiss监听
  9. dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
  10. @Override
  11. public void onDismiss(DialogInterface dialog) {
  12. // TODO Auto-generated method stub
  13. }
  14. });
  15. // 监听Key事件被传递给dialog
  16. dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
  17. @Override
  18. public boolean onKey(DialogInterface dialog, int keyCode,
  19. KeyEvent event) {
  20. // TODO Auto-generated method stub
  21. return false;
  22. }
  23. });
  24. // 监听cancel事件
  25. dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
  26. @Override
  27. public void onCancel(DialogInterface dialog) {
  28. // TODO Auto-generated method stub
  29. }
  30. });
  31. //设置可点击的按钮,最多有三个(默认情况下)
  32. dialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定",
  33. new DialogInterface.OnClickListener() {
  34. @Override
  35. public void onClick(DialogInterface dialog, int which) {
  36. // TODO Auto-generated method stub
  37. }
  38. });
  39. dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消",
  40. new DialogInterface.OnClickListener() {
  41. @Override
  42. public void onClick(DialogInterface dialog, int which) {
  43. // TODO Auto-generated method stub
  44. }
  45. });
  46. dialog.setButton(DialogInterface.BUTTON_NEUTRAL, "中立",
  47. new DialogInterface.OnClickListener() {
  48. @Override
  49. public void onClick(DialogInterface dialog, int which) {
  50. // TODO Auto-generated method stub
  51. }
  52. });
  53. dialog.setMessage("这是一个圆形进度条");
  54. dialog.show();
  55. new Thread(new Runnable() {
  56. @Override
  57. public void run() {
  58. // TODO Auto-generated method stub
  59. try {
  60. Thread.sleep(5000);
  61. // cancel和dismiss方法本质都是一样的,都是从屏幕中删除Dialog,唯一的区别是
  62. // 调用cancel方法会回调DialogInterface.OnCancelListener如果注册的话,dismiss方法不会回掉
  63. dialog.cancel();
  64. // dialog.dismiss();
  65. } catch (InterruptedException e) {
  66. // TODO Auto-generated catch block
  67. e.printStackTrace();
  68. }
  69. }
  70. }).start();
final ProgressDialog dialog = new ProgressDialog(this);
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);// 设置进度条的形式为圆形转动的进度条
dialog.setCancelable(true);// 设置是否可以通过点击Back键取消
dialog.setCanceledOnTouchOutside(false);// 设置在点击Dialog外是否取消Dialog进度条
dialog.setIcon(R.drawable.ic_launcher);//
// 设置提示的title的图标,默认是没有的,如果没有设置title的话只设置Icon是不会显示图标的
dialog.setTitle("提示");
// dismiss监听
dialog.setOnDismissListener(new DialogInterface.OnDismissListener() { @Override
public void onDismiss(DialogInterface dialog) {
// TODO Auto-generated method stub }
});
// 监听Key事件被传递给dialog
dialog.setOnKeyListener(new DialogInterface.OnKeyListener() { @Override
public boolean onKey(DialogInterface dialog, int keyCode,
KeyEvent event) {
// TODO Auto-generated method stub
return false;
}
});
// 监听cancel事件
dialog.setOnCancelListener(new DialogInterface.OnCancelListener() { @Override
public void onCancel(DialogInterface dialog) {
// TODO Auto-generated method stub }
});
//设置可点击的按钮,最多有三个(默认情况下)
dialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定",
new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub }
});
dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消",
new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub }
});
dialog.setButton(DialogInterface.BUTTON_NEUTRAL, "中立",
new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub }
});
dialog.setMessage("这是一个圆形进度条");
dialog.show();
new Thread(new Runnable() { @Override
public void run() {
// TODO Auto-generated method stub
try {
Thread.sleep(5000);
// cancel和dismiss方法本质都是一样的,都是从屏幕中删除Dialog,唯一的区别是
// 调用cancel方法会回调DialogInterface.OnCancelListener如果注册的话,dismiss方法不会回掉
dialog.cancel();
// dialog.dismiss();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
}).start();

其中通过Thread.sleep(5000)模拟后台操作。

cancel和dismiss方法本质都是一样的,都是从屏幕中删除Dialog,唯一的区别是:调用cancel方法会回调DialogInterface.OnCancelListener如果注册的话,dismiss方法不会回掉。

第二种方式:水平进度条

  1. // 进度条还有二级进度条的那种形式,这里就不演示了
  2. final ProgressDialog dialog = new ProgressDialog(this);
  3. dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);// 设置水平进度条
  4. dialog.setCancelable(true);// 设置是否可以通过点击Back键取消
  5. dialog.setCanceledOnTouchOutside(false);// 设置在点击Dialog外是否取消Dialog进度条
  6. dialog.setIcon(R.drawable.ic_launcher);// 设置提示的title的图标,默认是没有的
  7. dialog.setTitle("提示");
  8. dialog.setMax(100);
  9. dialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定",
  10. new DialogInterface.OnClickListener() {
  11. @Override
  12. public void onClick(DialogInterface dialog, int which) {
  13. // TODO Auto-generated method stub
  14. }
  15. });
  16. dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消",
  17. new DialogInterface.OnClickListener() {
  18. @Override
  19. public void onClick(DialogInterface dialog, int which) {
  20. // TODO Auto-generated method stub
  21. }
  22. });
  23. dialog.setButton(DialogInterface.BUTTON_NEUTRAL, "中立",
  24. new DialogInterface.OnClickListener() {
  25. @Override
  26. public void onClick(DialogInterface dialog, int which) {
  27. // TODO Auto-generated method stub
  28. }
  29. });
  30. dialog.setMessage("这是一个水平进度条");
  31. dialog.show();
  32. new Thread(new Runnable() {
  33. @Override
  34. public void run() {
  35. // TODO Auto-generated method stub
  36. int i = 0;
  37. while (i < 100) {
  38. try {
  39. Thread.sleep(200);
  40. // 更新进度条的进度,可以在子线程中更新进度条进度
  41. dialog.incrementProgressBy(1);
  42. // dialog.incrementSecondaryProgressBy(10)//二级进度条更新方式
  43. i++;
  44. } catch (Exception e) {
  45. // TODO: handle exception
  46. }
  47. }
  48. // 在进度条走完时删除Dialog
  49. dialog.dismiss();
  50. }
  51. }).start();
	// 进度条还有二级进度条的那种形式,这里就不演示了
final ProgressDialog dialog = new ProgressDialog(this);
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);// 设置水平进度条
dialog.setCancelable(true);// 设置是否可以通过点击Back键取消
dialog.setCanceledOnTouchOutside(false);// 设置在点击Dialog外是否取消Dialog进度条
dialog.setIcon(R.drawable.ic_launcher);// 设置提示的title的图标,默认是没有的
dialog.setTitle("提示");
dialog.setMax(100);
dialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定",
new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub }
});
dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消",
new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub }
});
dialog.setButton(DialogInterface.BUTTON_NEUTRAL, "中立",
new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub }
});
dialog.setMessage("这是一个水平进度条");
dialog.show();
new Thread(new Runnable() { @Override
public void run() {
// TODO Auto-generated method stub
int i = 0;
while (i < 100) {
try {
Thread.sleep(200);
// 更新进度条的进度,可以在子线程中更新进度条进度
dialog.incrementProgressBy(1);
// dialog.incrementSecondaryProgressBy(10)//二级进度条更新方式
i++; } catch (Exception e) {
// TODO: handle exception
}
}
// 在进度条走完时删除Dialog
dialog.dismiss(); }
}).start();

更多的是使用自定义的ProgressDialog,以满足不同显示的需要。

ProgressDialog使用总结的更多相关文章

  1. Android 之 ProgressDialog用法介绍

    布局文件测试: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" androi ...

  2. Android中通过线程实现更新ProgressDialog(对话进度条)

    作为开发者我们需要经常站在用户角度考虑问题,比如在应用商城下载软件时,当用户点击下载按钮,则会有下载进度提示页面出现,现在我们通过线程休眠的方式模拟下载进度更新的演示,如图(这里为了截图方便设置对话进 ...

  3. Android 屏幕旋转 处理 AsyncTask 和 ProgressDialog 的最佳方案

    的最佳方案 标签: Android屏幕旋转AsyncTaskProgressDialog 2014-07-19 09:25 39227人阅读 评论(46) 收藏 举报 分类: [android 进阶之 ...

  4. 进度条ProgressDialog

    1.效果图 public void click(View view) { final ProgressDialog pdDialog = new ProgressDialog(this); //设置标 ...

  5. 设立点击ProgressDialog外的区域对话框不消失

    设置点击ProgressDialog外的区域对话框不消失ProgressDialog mpDialog = new ProgressDialog(OrderTable.this);  mpDialog ...

  6. android ProgressDialog 正在载...Loading...

    final ProgressDialog pd = new ProgressDialog(mContext); pd.setMessage("正在加载..."); pd.show( ...

  7. ProgressDialog的使用

    ProgressDialog 继承自AlertDialog,AlertDialog继承自Dialog,实现DialogInterface接口. ProgressDialog的创建方式有两种,一种是ne ...

  8. Dialog , ProgressDialog , PopWindow 区别

    本质区别: Dialog:非阻塞对话框,弹出对话框时时,后台还可以做事情,点击背景时,对话框消失 ProgressDialog:带有圆形进度或者条形进度的对话框,一般结合handler使用.任务完成后 ...

  9. Android之alertDialog、ProgressDialog

    一.alertDialog 置顶于所有控件之上的,可以屏蔽其他控件的交互能力.通过AlertDialog.Builder创建一个AlertDialog,并通过setTittle(),setMesseg ...

随机推荐

  1. Struts学习总结-02 类型转换

    一 类型转换 input.jsp <%@ page language="java" import="java.util.*" pageEncoding=& ...

  2. linux系统性能监视命令

    preface as a linux engineer,you should know how to use these command of monitor system,so let's lear ...

  3. 【原】jquery图片预览

    平时我们在做图片上传的时候,如果可以让用户选择图片的时候,看到图片的效果,那这样用户体验会好很多,因为用户可以就可以决定是否继续用这张图片,尤其是和ajaxuploadfile结合使用的时候,图片的预 ...

  4. python学习笔记-(三)条件判断和循环

    1.条件判断语句 Python中条件选择语句的关键字为:if .elif .else这三个.其基本形式如下: age_of_cc = 27 age = int(input("guessage ...

  5. c#正则表达式2

    System.Text.RegularExpressions.Regex ___rx = new System.Text.RegularExpressions.Regex(@""& ...

  6. mysql_query()与mysql_real_query()

    mysql_query() cannot be used ” character, which mysql_query() interprets as the end of the statement ...

  7. Visual Studio无法查找或打开 PDB 文件解决办法

    Visual Studio无法查找或打开 PDB 文件解决办法 用VS调试程序时,有时会在VS底部的“输出”框中提示“无法查找或打开 PDB 文件”.这该怎么解决呢? 下面,我们以VS2013为例,来 ...

  8. Redis总结(三)Redis 的主从复制

    接着上一篇,前面两篇我总结了<Redis总结(一)Redis安装>和<Redis总结(二)C#中如何使用redis> 所以这一篇,会讲讲Redis 的主从复制以及C#中如何调用 ...

  9. centos 7.0 ssh 登陆

    CentOS 7.0 最小化安装 默认连接 默认端口是22 安装完查看IP 命令 ip addr ip: 192.168.1.103 自己家里练习的机子 都没改配置了 使用putty 远程连接 下载页 ...

  10. 在Centos上安装RabbitMQ流程(转)

    在Centos上安装RabbitMQ流程------------------------ 1. 需求 由于项目中要用到消息队列,经过ActiveMQ与RabbitMQ的比较,最终选择了RabbbitM ...