ProgressDialog学习

来源:互联网 发布:学为贵封闭班 知乎 编辑:程序博客网 时间:2024/06/08 18:25

ProgressDialog是用在耗时操作上的一种组件。

基本原理是新建一个线程去执行耗时操作,原线程执行 ProgressDialog对话框的绘制。

两种方式实现ProgressDialog

第一种方式是直接使用ProgressDialog pd = ProgressDialog.show(MainActivity.this, "Loading...", "Please wait...", true, false);函数,将标题和信息一次性给出,适用于简单对话框。

第二种是

ProgressDialog  pd = new ProgressDialog(MainActivity.this);
pd.setTitle("loading");
pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pd.setMessage("please wait,please~~");
pd.show();

除此之外ProgressDialog  还有很多方法设置对话框。

setProgressStyle的参数

1)ProgressDialog.STYLE_HORIZONTAL

int STYLE_HORIZONTAL Creates a ProgressDialog with a horizontal progress bar. 

2)ProgressDialog.STYLE_SPINNER 

int STYLE_SPINNER Creates a ProgressDialog with a circular, spinning progress bar.


最终结果:

三张图片依次为,初始状态,按下第一个按钮,按下第二个按钮。


xml文件

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7.   
  8. <Button android:id="@+id/btn"  
  9.     android:layout_width="fill_parent"  
  10.     android:layout_height="wrap_content"  
  11.     android:text="press to begin"/>  
  12. <Button android:id="@+id/btn2"  
  13.     android:layout_width="fill_parent"  
  14.     android:layout_height="wrap_content"  
  15.     android:text="press to begin,style"/>  
  16. <TextView android:id="@+id/txt"  
  17.     android:layout_width="fill_parent"   
  18.     android:layout_height="wrap_content"   
  19.     android:text="begin"  
  20.     />  
  21. </LinearLayout>  


MainActivity.java

[java] view plaincopy
  1. package com.xujin.progressbar;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.ProgressDialog;  
  5. import android.os.Bundle;  
  6. import android.os.Handler;  
  7. import android.os.Message;  
  8. import android.view.View;  
  9. import android.widget.Button;  
  10. import android.widget.TextView;  
  11.   
  12. public class MainActivity extends Activity {  
  13.       
  14.     private TextView txt;  
  15.     private Button Btn;  
  16.     private Button Btn2;  
  17.     private ProgressDialog pd;  
  18.       
  19.     @Override  
  20.     public void onCreate(Bundle savedInstanceState) {  
  21.         super.onCreate(savedInstanceState);  
  22.         setContentView(R.layout.activity_main);  
  23.         txt = (TextView)findViewById(R.id.txt);  
  24.         Btn = (Button)findViewById(R.id.btn);  
  25.         Btn2 = (Button)findViewById(R.id.btn2);  
  26.         Btn.setOnClickListener(new View.OnClickListener() {           
  27.             @Override  
  28.             public void onClick(View v) {                 
  29.                 pd = ProgressDialog.show(MainActivity.this"Loading...""Please wait..."truefalse);  
  30.                 new Thread(){  
  31.                     @Override  
  32.                     public void run() {  
  33.                         //需要花时间的函数  
  34.                         try {    
  35.                             Thread.sleep(5000);    
  36.                         } catch (InterruptedException e) {    
  37.                             // TODO Auto-generated catch block    
  38.                             e.printStackTrace();    
  39.                         }                         
  40.                         //向handler发消息  
  41.                         handler.sendEmptyMessage(0);  
  42.                     }}.start();  
  43.             }  
  44.         });  
  45.         Btn2.setOnClickListener(new View.OnClickListener() {              
  46.             @SuppressWarnings("deprecation")  
  47.             @Override  
  48.             public void onClick(View v) {                 
  49.                 pd = new ProgressDialog(MainActivity.this);  
  50.                 pd.setTitle("loading");  
  51.                 pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);  
  52.                 pd.setMessage("please wait,please~~");  
  53.                 pd.show();  
  54.                 new Thread(){  
  55.                     @Override  
  56.                     public void run() {  
  57.                         //需要花时间的函数  
  58.                         try {    
  59.                             Thread.sleep(5000);    
  60.                         } catch (InterruptedException e) {    
  61.                             // TODO Auto-generated catch block    
  62.                             e.printStackTrace();    
  63.                         }                         
  64.                         //向handler发消息  
  65.                         handler.sendEmptyMessage(0);  
  66.                     }}.start();  
  67.             }  
  68.         });  
  69.     }  
  70.       
  71.   
  72.     private Handler handler = new Handler(){  
  73.   
  74.         @Override  
  75.         public void handleMessage(Message msg) {  
  76.             pd.dismiss();             
  77.             txt.setText("End");  
  78.         }};      
  79. }  
0 0
原创粉丝点击