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

相关文章

Java注解之超越Javadoc的元数据利器详解

《Java注解之超越Javadoc的元数据利器详解》本文将深入探讨Java注解的定义、类型、内置注解、自定义注解、保留策略、实际应用场景及最佳实践,无论是初学者还是资深开发者,都能通过本文了解如何利用... 目录什么是注解?注解的类型内置注编程解自定义注解注解的保留策略实际用例最佳实践总结在 Java 编程

MySQL数据库约束深入详解

《MySQL数据库约束深入详解》:本文主要介绍MySQL数据库约束,在MySQL数据库中,约束是用来限制进入表中的数据类型的一种技术,通过使用约束,可以确保数据的准确性、完整性和可靠性,需要的朋友... 目录一、数据库约束的概念二、约束类型三、NOT NULL 非空约束四、DEFAULT 默认值约束五、UN

Python使用Matplotlib绘制3D曲面图详解

《Python使用Matplotlib绘制3D曲面图详解》:本文主要介绍Python使用Matplotlib绘制3D曲面图,在Python中,使用Matplotlib库绘制3D曲面图可以通过mpl... 目录准备工作绘制简单的 3D 曲面图绘制 3D 曲面图添加线框和透明度控制图形视角Matplotlib

MySQL中的分组和多表连接详解

《MySQL中的分组和多表连接详解》:本文主要介绍MySQL中的分组和多表连接的相关操作,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友一起看看吧... 目录mysql中的分组和多表连接一、MySQL的分组(group javascriptby )二、多表连接(表连接会产生大量的数据垃圾)MySQL中的

Java 实用工具类Spring 的 AnnotationUtils详解

《Java实用工具类Spring的AnnotationUtils详解》Spring框架提供了一个强大的注解工具类org.springframework.core.annotation.Annot... 目录前言一、AnnotationUtils 的常用方法二、常见应用场景三、与 JDK 原生注解 API 的

redis中使用lua脚本的原理与基本使用详解

《redis中使用lua脚本的原理与基本使用详解》在Redis中使用Lua脚本可以实现原子性操作、减少网络开销以及提高执行效率,下面小编就来和大家详细介绍一下在redis中使用lua脚本的原理... 目录Redis 执行 Lua 脚本的原理基本使用方法使用EVAL命令执行 Lua 脚本使用EVALSHA命令

SpringBoot3.4配置校验新特性的用法详解

《SpringBoot3.4配置校验新特性的用法详解》SpringBoot3.4对配置校验支持进行了全面升级,这篇文章为大家详细介绍了一下它们的具体使用,文中的示例代码讲解详细,感兴趣的小伙伴可以参考... 目录基本用法示例定义配置类配置 application.yml注入使用嵌套对象与集合元素深度校验开发

Python中的Walrus运算符分析示例详解

《Python中的Walrus运算符分析示例详解》Python中的Walrus运算符(:=)是Python3.8引入的一个新特性,允许在表达式中同时赋值和返回值,它的核心作用是减少重复计算,提升代码简... 目录1. 在循环中避免重复计算2. 在条件判断中同时赋值变量3. 在列表推导式或字典推导式中简化逻辑

Java Stream流使用案例深入详解

《JavaStream流使用案例深入详解》:本文主要介绍JavaStream流使用案例详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录前言1. Lambda1.1 语法1.2 没参数只有一条语句或者多条语句1.3 一个参数只有一条语句或者多

SpringBoot整合mybatisPlus实现批量插入并获取ID详解

《SpringBoot整合mybatisPlus实现批量插入并获取ID详解》这篇文章主要为大家详细介绍了SpringBoot如何整合mybatisPlus实现批量插入并获取ID,文中的示例代码讲解详细... 目录【1】saveBATch(一万条数据总耗时:2478ms)【2】集合方式foreach(一万条数