ProgressDialog用法详解

2023-12-22 10:58

本文主要是介绍ProgressDialog用法详解,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!



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



http://www.chinasem.cn/article/523691

相关文章

Spring Boot中的路径变量示例详解

《SpringBoot中的路径变量示例详解》SpringBoot中PathVariable通过@PathVariable注解实现URL参数与方法参数绑定,支持多参数接收、类型转换、可选参数、默认值及... 目录一. 基本用法与参数映射1.路径定义2.参数绑定&nhttp://www.chinasem.cnbs

MySql基本查询之表的增删查改+聚合函数案例详解

《MySql基本查询之表的增删查改+聚合函数案例详解》本文详解SQL的CURD操作INSERT用于数据插入(单行/多行及冲突处理),SELECT实现数据检索(列选择、条件过滤、排序分页),UPDATE... 目录一、Create1.1 单行数据 + 全列插入1.2 多行数据 + 指定列插入1.3 插入否则更

Redis中Stream详解及应用小结

《Redis中Stream详解及应用小结》RedisStreams是Redis5.0引入的新功能,提供了一种类似于传统消息队列的机制,但具有更高的灵活性和可扩展性,本文给大家介绍Redis中Strea... 目录1. Redis Stream 概述2. Redis Stream 的基本操作2.1. XADD

Spring StateMachine实现状态机使用示例详解

《SpringStateMachine实现状态机使用示例详解》本文介绍SpringStateMachine实现状态机的步骤,包括依赖导入、枚举定义、状态转移规则配置、上下文管理及服务调用示例,重点解... 目录什么是状态机使用示例什么是状态机状态机是计算机科学中的​​核心建模工具​​,用于描述对象在其生命

Java JDK1.8 安装和环境配置教程详解

《JavaJDK1.8安装和环境配置教程详解》文章简要介绍了JDK1.8的安装流程,包括官网下载对应系统版本、安装时选择非系统盘路径、配置JAVA_HOME、CLASSPATH和Path环境变量,... 目录1.下载JDK2.安装JDK3.配置环境变量4.检验JDK官网下载地址:Java Downloads

使用Python删除Excel中的行列和单元格示例详解

《使用Python删除Excel中的行列和单元格示例详解》在处理Excel数据时,删除不需要的行、列或单元格是一项常见且必要的操作,本文将使用Python脚本实现对Excel表格的高效自动化处理,感兴... 目录开发环境准备使用 python 删除 Excphpel 表格中的行删除特定行删除空白行删除含指定

全面掌握 SQL 中的 DATEDIFF函数及用法最佳实践

《全面掌握SQL中的DATEDIFF函数及用法最佳实践》本文解析DATEDIFF在不同数据库中的差异,强调其边界计算原理,探讨应用场景及陷阱,推荐根据需求选择TIMESTAMPDIFF或inte... 目录1. 核心概念:DATEDIFF 究竟在计算什么?2. 主流数据库中的 DATEDIFF 实现2.1

MySQL中的LENGTH()函数用法详解与实例分析

《MySQL中的LENGTH()函数用法详解与实例分析》MySQLLENGTH()函数用于计算字符串的字节长度,区别于CHAR_LENGTH()的字符长度,适用于多字节字符集(如UTF-8)的数据验证... 目录1. LENGTH()函数的基本语法2. LENGTH()函数的返回值2.1 示例1:计算字符串

Spring Boot spring-boot-maven-plugin 参数配置详解(最新推荐)

《SpringBootspring-boot-maven-plugin参数配置详解(最新推荐)》文章介绍了SpringBootMaven插件的5个核心目标(repackage、run、start... 目录一 spring-boot-maven-plugin 插件的5个Goals二 应用场景1 重新打包应用

mybatis执行insert返回id实现详解

《mybatis执行insert返回id实现详解》MyBatis插入操作默认返回受影响行数,需通过useGeneratedKeys+keyProperty或selectKey获取主键ID,确保主键为自... 目录 两种方式获取自增 ID:1. ​​useGeneratedKeys+keyProperty(推