(转)ProgressDialog用法详解

转载自:

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 style="">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 style="">    }  
  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秒", true, false);  
  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>  

 

posted @ 2016-05-11 21:35  流浪随风  阅读(2399)  评论(0编辑  收藏  举报