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...", true, false);  
  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收到消息中,它会把它放入到队列中等待执行,通常来说这会很快被执行。

原文地址:https://www.cnblogs.com/xgjblog/p/4030475.html