Android 之 ProgressDialog

ProgressDialog 类似于ProgressBar,都是用于显示进度,区别是一个是控件一个是对话框。

ProgressDialog有两种形式一个是STYLE_SPINNER,STYLE_HORIZONTAL。

其中前者没有固定值,后者可以设置最大值和当前值。

 

下面直接给出一个实例。

 

 

 

  1. package com.rocky.demo;  
  2. import android.app.Activity;  
  3. import android.app.ProgressDialog;  
  4. import android.content.DialogInterface;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.widget.Button;  
  9. public class ProgressDialogDemo extends Activity {  
  10.     /** Called when the activity is first created. */  
  11.     @Override  
  12.     public void onCreate(Bundle savedInstanceState) {  
  13.         super.onCreate(savedInstanceState);  
  14.         setContentView(R.layout.main);  
  15.           
  16.         ((Button)this.findViewById(R.id.Button01)).setOnClickListener(btn1Clicker);  
  17.         ((Button)this.findViewById(R.id.Button02)).setOnClickListener(btn2Clicker);  
  18.                   
  19.     }  
  20.     int m_count=0;  
  21.     ProgressDialog prDialog;  
  22.     OnClickListener btn1Clicker=new OnClickListener()  
  23.     {  
  24.         @Override  
  25.         public void onClick(View v) {  
  26.             // TODO Auto-generated method stub  
  27.               
  28.             prDialog=new ProgressDialog(ProgressDialogDemo.this);  
  29.             prDialog.setTitle("提示信息");  
  30.             prDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);  
  31.             prDialog.setMessage("This is a cicle process dialog ");  
  32.             prDialog.setIcon(R.drawable.icon);  
  33.             //进度条是否不明确  
  34.             prDialog.setIndeterminate(true);  
  35.             prDialog.setCancelable(true);  
  36.             prDialog.setButton("确定"new DialogInterface.OnClickListener(){  
  37.                 @Override  
  38.                 public void onClick(DialogInterface dialog, int which) {  
  39.                     // TODO Auto-generated method stub  
  40.                       
  41.                     prDialog.cancel();  
  42.                       
  43.                 }                 
  44.             });           
  45.               
  46.             prDialog.show();              
  47.         }  
  48.           
  49.     };  
  50.     OnClickListener btn2Clicker=new OnClickListener()  
  51.     {  
  52.         @Override  
  53.         public void onClick(View v) {  
  54.             // TODO Auto-generated method stub  
  55.               
  56.             m_count=0;  
  57.               
  58.             prDialog=new ProgressDialog(ProgressDialogDemo.this);  
  59.             prDialog.setTitle("提示信息");  
  60.             prDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);  
  61.             prDialog.setMessage("This is a cicle process dialog ");  
  62.             prDialog.setIcon(R.drawable.icon);  
  63.             //进度条是否不明确  
  64.             prDialog.setIndeterminate(false);  
  65.             prDialog.setCancelable(true);  
  66.             prDialog.setProgress(200);  
  67.             prDialog.setMax(200);  
  68.             prDialog.setButton("确定"new DialogInterface.OnClickListener(){  
  69.                 @Override  
  70.                 public void onClick(DialogInterface dialog, int which) {  
  71.                     // TODO Auto-generated method stub  
  72.                       
  73.                     prDialog.cancel();  
  74.                       
  75.                 }                 
  76.             });           
  77.               
  78.             prDialog.show();  
  79.             new Thread()  
  80.             {  
  81.                 @Override  
  82.                 public void run() {  
  83.                     // TODO Auto-generated method stub  
  84.                     //super.run();  
  85.                   
  86.                     try  
  87.                     {  
  88.                         while(m_count<=200)  
  89.                         {  
  90.                             prDialog.setProgress(m_count++);  
  91.                             Thread.sleep(100);  
  92.                               
  93.                         }  
  94.                         prDialog.cancel();  
  95.                     }  
  96.                     catch(InterruptedException e)  
  97.                     {  
  98.                         e.printStackTrace();  
  99.                     }  
  100.       
  101.                   
  102.                   
  103.                 }  
  104.                   
  105.             }.start();  
  106.               
  107.               
  108.         }  
  109.           
  110.     };  
  111. }  

 

 

<!--EndFragment-->

你可能感兴趣的:(ProgressDialog)