Android中ProgressDialog的簡單示例

網上一般對進度條的示例都是如何顯示,沒有在任務結束如何關閉的文章,參考其他文章經過試驗之後把整套進度條顯示的簡單示例如下: 

建立android工程等工作都略去,Google一下就可以了。 

下面來介紹主要的Activity 
ProgressBarDemo.java 
Java代碼  收藏代碼
  1. package com.lveyo.android.demo.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 ProgressBarDemo extends Activity {  
  13.       
  14.     private TextView statusTextView;  
  15.     private Button beginBtn;  
  16.     private ProgressDialog progressDialog;  
  17.       
  18.     @Override  
  19.     public void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.         setContentView(R.layout.main);  
  22.         statusTextView = (TextView)findViewById(R.id.status);  
  23.         beginBtn = (Button)findViewById(R.id.beginBtn);  
  24.         setListener();  
  25.     }  
  26.       
  27.     /** 
  28.      * 用Handler來更新UI 
  29.      */  
  30.     private Handler handler = new Handler(){  
  31.   
  32.         @Override  
  33.         public void handleMessage(Message msg) {  
  34.               
  35.             //關閉ProgressDialog  
  36.             progressDialog.dismiss();  
  37.               
  38.             //更新UI  
  39.             statusTextView.setText("Completed!");  
  40.         }};  
  41.       
  42.           
  43.     /** 
  44.      * 點擊按鈕事件listener 
  45.      */  
  46.     private void setListener(){  
  47.         beginBtn.setOnClickListener(new View.OnClickListener() {  
  48.               
  49.             @Override  
  50.             public void onClick(View v) {  
  51.                   
  52.                 //顯示ProgressDialog  
  53.                 progressDialog = ProgressDialog.show(ProgressBarDemo.this"Loading...""Please wait..."truefalse);  
  54.                   
  55.                 //新建線程  
  56.                 new Thread(){  
  57.   
  58.                     @Override  
  59.                     public void run() {  
  60.                         //需要花時間計算的方法  
  61.                         Calculation.calculate(4);  
  62.                           
  63.                         //向handler發消息  
  64.                         handler.sendEmptyMessage(0);  
  65.                     }}.start();  
  66.             }  
  67.         });  
  68.     }  
  69.       
  70. }  


Calculation.java 
Java代碼  收藏代碼
  1. package com.lveyo.android.demo.progressbar;  
  2.   
  3. /** 
  4.  * 示意方法 
  5.  * @author lveyo 
  6.  * 
  7.  */  
  8. public class Calculation {  
  9.       
  10.     public static void calculate(int sleepSeconds){  
  11.         try {  
  12.             Thread.sleep(sleepSeconds * 1000);  
  13.         } catch (Exception e) {  
  14.             // TODO: handle exception  
  15.         }  
  16.     }  
  17.   
  18. }  



main.xml文件 
Xml代碼  收藏代碼
  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. <TextView android:id="@+id/status"  
  8.     android:layout_width="fill_parent"   
  9.     android:layout_height="wrap_content"   
  10.     android:text="@string/hello"  
  11.     />  
  12. <Button android:id="@+id/beginBtn"  
  13.     android:layout_width="fill_parent"  
  14.     android:layout_height="wrap_content"  
  15.     android:text="begin"  
  16.     />  
  17. </LinearLayout>  


在android中,通常我們無法在單獨的線程中更新UI,而要在主線程中,這也就是爲什麼我們要使用 Handler了,當handler收到消息中,它會把它放入到隊列中等待執行,通常來說這會很快被執行。 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章