ProgressDialog的基本用法

ProgressDialog为进度对话框。android手机自带的对话框显得比较单一,我们可以通过ProgressDialog来自己定义对话框中将要显示出什么东西。

首先看看progressDialog里面的方法

setProgressStyle:设置进度条风格,风格为圆形,旋转的。
  setTitlt:设置标题
  setMessage:设置提示信息;
  setIcon:设置标题图标;
  setIndeterminate:设置ProgressDialog 的进度条是否不明确;这个属性对于ProgressDailog默认的转轮模式没有实际意义,默认下设置为true,它仅仅对带有ProgressBar的Dialog有作用。修改这个属性为false后可以实时更新进度条的进度。
  setCancelable:设置ProgressDialog 是否可以按返回键取消;

CancelListner:当前Dialog强制取消之后将会被执行,通常用来清理未完成的任务。
  setButton:设置ProgressDialog 的一个Button(需要监听Button事件);
  show:显示ProgressDialog。

cancel:删除progressdialog

dismiss: 删除progressdialog 作用和cancel相同

setProgress(intCounter);更新进度条,当然一般都需要Handler的结合来更新进度条

<!--EndFragment-->

然后我们看看效果

 

 

  实现代码如下

Java代码  
  1. <span>package cn.android;
  2. import android.app.Activity;
  3. import android.app.ProgressDialog;
  4. import android.content.DialogInterface;
  5. import android.content.Intent;
  6. import android.os.Bundle;
  7. import android.os.Handler;
  8. import android.os.Message;
  9. import android.view.View;
  10. import android.view.View.OnClickListener;
  11. import android.widget.Button;
  12. import android.widget.Toast;
  13. public class SecondActivity extends Activity implements Runnable{
  14. /**
  15. * Called when the activity is first created.
  16. * Activity入口
  17. * */
  18. private Button b_dialog,b_dialog1,button;//两个按钮
  19. private ProgressDialog pd,pd1;//进度条对话框
  20. private int count;
  21. public void onCreate(Bundle savedInstanceState) {
  22. super.onCreate(savedInstanceState);
  23. setContentView(R.layout.second);//关联对应的界面
  24. b_dialog = (Button)this.findViewById(R.id.button_dialog);
  25. b_dialog1 = (Button)this.findViewById(R.id.Button_dialog1);
  26. //处理事件发生时要做的事
  27. b_dialog.setOnClickListener(listener);
  28. b_dialog1.setOnClickListener(listener);
  29. </span>
  30. <span>    }
  31. //定义监听器对象
  32. private OnClickListener listener = new OnClickListener() {
  33. // 鼠标按下后
  34. public void onClick(View v) {
  35. // 得到当前被触发的事件的ID —— 类型是int
  36. int id = v.getId();
  37. if(id == R.id.button_dialog){
  38. //按下确定键就会消失的进程对话框
  39. //             pd = new ProgressDialog(SecondActivity.this);// 创建ProgressDialog对象
  40. //             pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);// 设置进度条风格,风格为圆形,旋转的
  41. //             pd.setTitle("提示");// 设置ProgressDialog 标题
  42. //             pd.setMessage("这是一个圆形进度条对话框");// 设置ProgressDialog提示信息
  43. //             pd.setIcon(R.drawable.icon);// 设置ProgressDialog标题图标
  44. //             // 设置ProgressDialog 的进度条是否不明确 false 就是不设置为不明确
  45. //             pd.setIndeterminate(false);
  46. //             pd.setCancelable(true); // 设置ProgressDialog 是否可以按退回键取消
  47. //             pd.setButton("确定", new Bt1DialogListener()); // 设置ProgressDialog 的一个Button
  48. //             pd.show(); // 让ProgressDialog显示
  49. //过1秒钟就会自己消失的进程对话框
  50. //弹出另外一种对话框
  51. pd = ProgressDialog.show(SecondActivity.this, "自动关闭对话框", "Working,,,,,,1秒", true, false);
  52. Thread thread = new Thread(SecondActivity.this);//开启一个线程来延时
  53. thread.start();//启动线程
  54. }else if(id == R.id.Button_dialog1){
  55. pd1 = new ProgressDialog(SecondActivity.this);// 创建ProgressDialog对象
  56. pd1.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);// 设置进度条风格,风格为圆形,旋转的
  57. pd1.setTitle("提示");// 设置ProgressDialog 标题
  58. pd1.setMessage("这是一个条状进度条对话框");// 设置ProgressDialog提示信息
  59. pd1.setIcon(R.drawable.secondback);// 设置ProgressDialog标题图标
  60. // 设置ProgressDialog 的进度条是否不明确 false 就是不设置为不明确
  61. pd1.setIndeterminate(false);
  62. pd1.setCancelable(true); // 设置ProgressDialog 是否可以按退回键取消
  63. pd1.setProgress(100);// 设置ProgressDialog 进度条进度
  64. pd1.show(); // 让ProgressDialog显示
  65. count = 0;
  66. new Thread() {
  67. public void run() {
  68. try {
  69. while(count <= 100) {
  70. // 由线程来控制进度
  71. pd1.setProgress(count++);
  72. Thread.sleep(100);
  73. }
  74. pd1.cancel();
  75. } catch (Exception e) {
  76. pd1.cancel();
  77. }
  78. }
  79. }.start();
  80. }
  81. }
  82. };
  83. //run的是实现
  84. public void run() {
  85. try {
  86. Thread.sleep(1000);//睡1秒
  87. } catch (InterruptedException e) {
  88. e.printStackTrace();
  89. }
  90. handler.sendEmptyMessage(0);//传送消息
  91. }
  92. //定义处理消息的对象
  93. private Handler handler = new Handler(){
  94. //加入传消息来了就这么么办
  95. public void handleMessage(Message msg){
  96. pd.dismiss();//对话框消失
  97. Toast.makeText(SecondActivity.this, "对话框就消失了", 3).show();
  98. }
  99. };
  100. // pdButton01的监听器类
  101. class Bt1DialogListener implements DialogInterface.OnClickListener {
  102. @Override
  103. public void onClick(DialogInterface dialog, int which) {
  104. // 点击“确定”按钮取消对话框
  105. dialog.cancel();
  106. }
  107. }
  108. }
  109. </span>
<span abp="600">package cn.android;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;public class SecondActivity extends Activity implements Runnable{/** * Called when the activity is first created. * Activity入口* */private Button b_dialog,b_dialog1,button;//两个按钮private ProgressDialog pd,pd1;//进度条对话框private int count;public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.second);//关联对应的界面b_dialog = (Button)this.findViewById(R.id.button_dialog);    b_dialog1 = (Button)this.findViewById(R.id.Button_dialog1);    //处理事件发生时要做的事b_dialog.setOnClickListener(listener);b_dialog1.setOnClickListener(listener);
</span>
<span abp="601">    }//定义监听器对象private OnClickListener listener = new OnClickListener() {// 鼠标按下后public void onClick(View v) {// 得到当前被触发的事件的ID —— 类型是intint id = v.getId();if(id == R.id.button_dialog){
//按下确定键就会消失的进程对话框
//             pd = new ProgressDialog(SecondActivity.this);// 创建ProgressDialog对象
//             pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);// 设置进度条风格,风格为圆形,旋转的
//             pd.setTitle("提示");// 设置ProgressDialog 标题
//             pd.setMessage("这是一个圆形进度条对话框");// 设置ProgressDialog提示信息
//             pd.setIcon(R.drawable.icon);// 设置ProgressDialog标题图标
//             // 设置ProgressDialog 的进度条是否不明确 false 就是不设置为不明确
//             pd.setIndeterminate(false);
//             pd.setCancelable(true); // 设置ProgressDialog 是否可以按退回键取消
//             pd.setButton("确定", new Bt1DialogListener()); // 设置ProgressDialog 的一个Button
//             pd.show(); // 让ProgressDialog显示    //过1秒钟就会自己消失的进程对话框//弹出另外一种对话框pd = ProgressDialog.show(SecondActivity.this, "自动关闭对话框", "Working,,,,,,1秒", true, false);Thread thread = new Thread(SecondActivity.this);//开启一个线程来延时               thread.start();//启动线程}else if(id == R.id.Button_dialog1){pd1 = new ProgressDialog(SecondActivity.this);// 创建ProgressDialog对象   pd1.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);// 设置进度条风格,风格为圆形,旋转的           pd1.setTitle("提示");// 设置ProgressDialog 标题   pd1.setMessage("这是一个条状进度条对话框");// 设置ProgressDialog提示信息       pd1.setIcon(R.drawable.secondback);// 设置ProgressDialog标题图标       // 设置ProgressDialog 的进度条是否不明确 false 就是不设置为不明确   pd1.setIndeterminate(false);                 pd1.setCancelable(true); // 设置ProgressDialog 是否可以按退回键取消            pd1.setProgress(100);// 设置ProgressDialog 进度条进度    pd1.show(); // 让ProgressDialog显示  count = 0;                                 new Thread() {     public void run() {   try {   while(count <= 100) {   // 由线程来控制进度   pd1.setProgress(count++);   Thread.sleep(100);   }   pd1.cancel();   } catch (Exception e) {   pd1.cancel();   }   }   }.start();   }}};//run的是实现public void run() {try {Thread.sleep(1000);//睡1秒} catch (InterruptedException e) {e.printStackTrace();}handler.sendEmptyMessage(0);//传送消息}//定义处理消息的对象private Handler handler = new Handler(){//加入传消息来了就这么么办public void handleMessage(Message msg){pd.dismiss();//对话框消失Toast.makeText(SecondActivity.this, "对话框就消失了", 3).show(); }};// pdButton01的监听器类   class Bt1DialogListener implements DialogInterface.OnClickListener {   @Override  public void onClick(DialogInterface dialog, int which) {   // 点击“确定”按钮取消对话框   dialog.cancel();   }   }   }
</span>

ProgressDialog用法详解相关推荐

  1. python argv 详解_Python3 sys.argv[ ]用法详解

    sys.argv[]说白了就是一个从程序外部获取参数的桥梁,这个"外部"很关键,因为我们从外部取得的参数可以是多个,所以获得的是一个列表(list),也就是说sys.argv其实可 ...

  2. oracle中的exists 和 not exists 用法详解

    from:http://blog.sina.com.cn/s/blog_601d1ce30100cyrb.html oracle中的exists 和 not exists 用法详解 (2009-05- ...

  3. ROW_NUMBER() OVER()函数用法详解 (分组排序 例子多)

    ROW_NUMBER() OVER()函数用法详解 (分组排序 例子多) https://blog.csdn.net/qq_25221835/article/details/82762416 post ...

  4. python的继承用法_【后端开发】python中继承有什么用法?python继承的用法详解

    本篇文章给大家带来的内容是关于python中继承有什么用法?python继承的用法详解,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. 面向对象三大特征 1.封装:根据职责将属性和方法 ...

  5. C++中substr()函数用法详解

    C++中substr()函数用法详解 原型: string substr (size_t pos = 0, size_t len = npos) const; 返回一个新构造的string对象,其值初 ...

  6. php theme_path,PHP_Yii2主题(Theme)用法详解,本文实例讲述了Yii2主题(Theme) - phpStudy

    Yii2主题(Theme)用法详解 本文实例讲述了Yii2主题(Theme)用法.分享给大家供大家参考,具体如下: 首先看看主要的配置方式: 'components' => [ 'view' = ...

  7. LayoutInflater的inflate函数用法详解

    LayoutInflater的inflate函数用法详解 LayoutInflater作用是将layout的xml布局文件实例化为View类对象. 获取LayoutInflater的方法有如下三种: ...

  8. Ext.Net学习笔记22:Ext.Net Tree 用法详解

    上面的图片是一个简单的树,使用Ext.Net来创建这样的树结构非常简单,代码如下: <ext:TreePanel runat="server"><Root> ...

  9. WinDbg用法详解

    WinDbg用法详解 对WinDbg的方方面面作了详细的讲解. 转载于:https://blog.51cto.com/laokaddk/125111

最新文章

  1. Ubuntu 搭建 Zerotier One MOON 根目录服务器
  2. 12个常用的JavaScript技巧
  3. vue中阻止冒泡事件
  4. Transformer 在美团搜索排序中的实践
  5. IP地址概念及其划分
  6. Android的DataBinding原理介绍
  7. 怎么改字段名称_精装房这么改!换门框,封阳台,效果出来比毛坯房还好
  8. Jquery实战——横纵向的菜单
  9. java登录验证技术,login_required -- 登录验证
  10. 获取进程列表和结束进程
  11. 爬虫_淘宝(selenium)
  12. Ubuntu安装redis客户端工具及简单使用
  13. 用java设计族谱家谱制_家谱网站的设计与实现.doc
  14. 添加178源未能链接服务器,添加178源_在cydia中添加178源_178插件源-Guide信息网
  15. html是l面包屑效果,CSS制作面包屑
  16. 华为鸿蒙os今日新闻,华为鸿蒙 HarmonyOS 2 来了!完整升级名单曝光
  17. 魔性计时器html6,最近抖音很火的6首BGM,太有魔性了!
  18. js中isNaN、Number.isNaN,isFinite、Number.isFinite的区别
  19. 将OPenCV的Dll 动态连接库改名字,变成自己想要的动态连接库名字
  20. 商品租赁系统(Java_类/接口/继承/多态)

热门文章

  1. HALCON双目标定
  2. python中attr_python中hasattr()、getattr()、setattr()函数的使用
  3. 安卓心理测试实训代码_我是如何从测试转到开发的
  4. maya如何查看资源大纲_3DMaya大纲视图在哪查看?
  5. python的变量对大小写并不敏感_Robot Framework 内置变量
  6. 一起来学习 WebRTC (篇一)| 掘金技术征文
  7. 如何在 Kubernetes 中对无状态应用进行分批发布
  8. iPhone销量低迷,或导致苹果放弃自动驾驶项目?
  9. Unity3D开发——LeRunning的人物角色信息的显示
  10. 将Button等控件嵌入到repeater中