Android入门之ProgressDialog(纯Java)


注意:

1、ProgressDialog.STYLE_SPINNER为圆形不确定进度条

2、ProgressDialog.STYLE_HORIZONTAL为条形进图条,当设置为可确定进度的进度条时,在调用show()函数之前设置进度无效,百分比始终为0。第二进度可设置,但是在show()之前设置没什么意义。不管是静态显示抑或是动态显示,都要在show()函数调用之后再操作。

    

  1. import Android.app.Activity;  
  2. import android.app.ProgressDialog;  
  3. import android.os.Bundle;  
  4. import android.view.View;  
  5. import android.view.View.OnClickListener;  
  6. import android.widget.*;  
  7.   
  8. public class ProgressDialogTest extends Activity {  
  9.   
  10.     private RelativeLayout mainView=null;  
  11.     private Button button1=null;  
  12.     private Button button2=null;  
  13.     private ProgressDialog pd1=null;  
  14.     private ProgressDialog pd2=null;  
  15.       
  16.     public void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         mainView=new RelativeLayout(this);  
  19.         button1=new Button(this);  
  20.         RelativeLayout.LayoutParams lp1=new RelativeLayout.LayoutParams(-2,-2);  
  21.         lp1.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);  
  22.         lp1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);  
  23.         button1.setLayoutParams(lp1);  
  24.         button1.setText("圆形进度条");  
  25.         button1.setOnClickListener(new OnClickListener(){  
  26.             public void onClick(View v) {  
  27.                 if(pd1==null){  
  28.                     pd1_init();  
  29.                 }  
  30.                 pd1.show();  
  31.             }  
  32.         });  
  33.         mainView.addView(button1);  
  34.         button2=new Button(this);  
  35.         RelativeLayout.LayoutParams lp2=new RelativeLayout.LayoutParams(-2,-2);  
  36.         lp2.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);  
  37.         lp2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);  
  38.         button2.setLayoutParams(lp2);  
  39.         button2.setText("条形进度条");  
  40.         button2.setOnClickListener(new OnClickListener(){  
  41.             public void onClick(View v) {  
  42.                 if(pd2==null){  
  43.                     pd2_init();  
  44.                 }  
  45.                 pd2.show();  
  46.                 pd2.setProgress(50);  
  47.                 /*一种动态加载效果 
  48.                 new Thread() {    
  49.                     int progress=0;  
  50.                     public void run() {    
  51.                         try {    
  52.                             while (progress <= 100) {    
  53.                                 // 由线程来控制进度    
  54.                                 pd2.setProgress(progress++);    
  55.                                 Thread.sleep(100);    
  56.                             }    
  57.                             pd2.cancel();    
  58.                         } catch (Exception e) {    
  59.                             pd2.cancel();    
  60.                         }    
  61.                     }    
  62.                 }.start();    
  63.                 */  
  64.             }  
  65.         });  
  66.         mainView.addView(button2);  
  67.         setContentView(mainView);  
  68.     }  
  69.     /*pd1初始化*/  
  70.     void pd1_init(){  
  71.         pd1=new ProgressDialog(this);  
  72.         pd1.setIcon(android.R.drawable.ic_menu_info_details);  
  73.         pd1.setTitle("正在联网");  
  74.         pd1.setProgressStyle(ProgressDialog.STYLE_SPINNER);  
  75.         pd1.setMessage("请稍后……");  
  76.     }  
  77.     /*pd2初始化*/  
  78.     void pd2_init(){  
  79.         pd2=new ProgressDialog(this);  
  80.         pd2.setIcon(android.R.drawable.sym_def_app_icon);  
  81.         pd2.setTitle("正在下载软件");  
  82.         pd2.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);  
  83.         pd2.setIndeterminate(false);  
  84.         pd2.setMessage("测试");  
  85.         pd2.setMax(100);  
  86.         //pd2.setProgress(50);   在show()之前设置该值无效,show()之前固定进度为0   
  87.         //pd2.setSecondaryProgress(75);//在show()之前有效,但是意义不大   
  88.     }  
  89. }  

相关内容