Android 基础教程之-------Android ProgressBar的使用!

大家好,今天给大家讲一下Android中ProgressBar的使用,这个控件在开发中还是经常用到的,比如用户浏览网页的时候,中间肯定有个传输过程,所以用进度条让用户耐心等待。再比如我在Android Market下载应用,它也会有进度条显示下载进度。我为什么要举这两个例子呢,是因为它们分别是圆形进度条和长形进度条的代表。如下面两张图:

Android 基础教程之-------Android ProgressBar的使用!_第1张图片

图1.UC浏览网页圆形进度条效果。

 

Android 基础教程之-------Android ProgressBar的使用!_第2张图片

图2.Google Market应用下载长形进度条效果.

 

下面我们看一下两都皆有之的Android自带的浏览器的效果图如下:

Android 基础教程之-------Android ProgressBar的使用!_第3张图片

为了让大家更容易理解我将简单写一个Demo,大家可以按照我的步骤一步一步来。

第一步:新建一个Android工程命名为ProgressBarDemo.

 

第二步:修改main.xml代码如下(圆形进度条和长形进度条这里样式不同用系统自带的):

[c-sharp] view plain copy print ?
  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   
  8.          android:layout_width="fill_parent"    
  9.          android:layout_height="wrap_content"   
  10.          android:text="Welcome to Mr Wei's blog" />    
  11.      <ProgressBar   
  12.          android:id="@+id/rectangleProgressBar"    
  13.          style="?android:attr/progressBarStyleHorizontal" mce_style="?android:attr/progressBarStyleHorizontal"     
  14.          android:layout_width="fill_parent"   
  15.          android:layout_height="wrap_content"  
  16.          android:visibility="gone"  
  17.          />  
  18.      <ProgressBar   
  19.          android:id="@+id/circleProgressBar"    
  20.          style="?android:attr/progressBarStyleLarge" mce_style="?android:attr/progressBarStyleLarge"  
  21.          android:layout_width="wrap_content"   
  22.          android:layout_height="wrap_content"  
  23.          android:visibility="gone"   
  24.          />  
  25.         
  26.      <Button android:id="@+id/button"   
  27.              android:text="Show ProgressBar"    
  28.              android:layout_width="wrap_content"   
  29.              android:layout_height="wrap_content"  
  30.              />  
  31.  </LinearLayout>   
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Welcome to Mr Wei's blog" /> <ProgressBar android:id="@+id/rectangleProgressBar" style="?android:attr/progressBarStyleHorizontal" mce_style="?android:attr/progressBarStyleHorizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:visibility="gone" /> <ProgressBar android:id="@+id/circleProgressBar" style="?android:attr/progressBarStyleLarge" mce_style="?android:attr/progressBarStyleLarge" android:layout_width="wrap_content" android:layout_height="wrap_content" android:visibility="gone" /> <Button android:id="@+id/button" android:text="Show ProgressBar" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>

第三步:修改ProgressBarDemo.java代码如下(如果这里有对Handler和Message不明白的地方可以去Android高手进阶教程(九) 看下这个教程):

[c-sharp] view plain copy print ?
  1. package com.tutor.progress;  
  2. import android.app.Activity;  
  3. import android.os.Bundle;  
  4. import android.os.Handler;  
  5. import android.os.Message;  
  6. import android.view.View;  
  7. import android.widget.Button;  
  8. import android.widget.ProgressBar;  
  9. public class ProgressBarDemo extends Activity {  
  10.       
  11.     private ProgressBar rectangleProgressBar,circleProgressBar;  
  12.     private Button mButton;  
  13.       
  14.     protected static final int STOP = 0x10000;  
  15.     protected static final int NEXT = 0x10001;  
  16.     private int iCount = 0;  
  17.       
  18.     public void onCreate(Bundle savedInstanceState) {  
  19.         super.onCreate(savedInstanceState);  
  20.         setContentView(R.layout.main);  
  21.         //findView By id   
  22.         rectangleProgressBar = (ProgressBar)findViewById(R.id.rectangleProgressBar);  
  23.         circleProgressBar = (ProgressBar)findViewById(R.id.circleProgressBar);  
  24.         mButton = (Button)findViewById(R.id.button);  
  25.           
  26.         rectangleProgressBar.setIndeterminate(false);  
  27.         circleProgressBar.setIndeterminate(false);  
  28.           
  29.         mButton.setOnClickListener(new Button.OnClickListener() {  
  30.               
  31.             public void onClick(View v) {  
  32.                   
  33.                 rectangleProgressBar.setVisibility(View.VISIBLE);  
  34.                 circleProgressBar.setVisibility(View.VISIBLE);  
  35.                   
  36.                 rectangleProgressBar.setMax(100);  
  37.                 rectangleProgressBar.setProgress(0);  
  38.                 circleProgressBar.setProgress(0);  
  39.                   
  40.                 //创建一个线程,每秒步长为5增加,到100%时停止  
  41.                 Thread mThread = new Thread(new Runnable() {  
  42.                       
  43.                     public void run() {  
  44.                           
  45.                         for(int i=0 ; i < 20; i++){  
  46.                             try{  
  47.                                 iCount = (i + 1) * 5;  
  48.                                 Thread.sleep(1000);  
  49.                                 if(i == 19){  
  50.                                     Message msg = new Message();  
  51.                                     msg.what = STOP;  
  52.                                     mHandler.sendMessage(msg);  
  53.                                     break;  
  54.                                 }else{  
  55.                                     Message msg = new Message();  
  56.                                     msg.what = NEXT;  
  57.                                     mHandler.sendMessage(msg);  
  58.                                 }  
  59.                             }catch (Exception e) {  
  60.                                 e.printStackTrace();  
  61.                             }  
  62.                         }  
  63.                           
  64.                     }  
  65.                 });  
  66.                 mThread.start();  
  67.             }  
  68.         });  
  69.     }  
  70.       
  71.     //定义一个Handler   
  72.     private Handler mHandler = new Handler(){  
  73.         public void handleMessage(Message msg){  
  74.             switch (msg.what) {  
  75.             case STOP:  
  76.                 rectangleProgressBar.setVisibility(View.GONE);  
  77.                 circleProgressBar.setVisibility(View.GONE);  
  78.                 Thread.currentThread().interrupt();  
  79.                 break;  
  80.             case NEXT:  
  81.                 if(!Thread.currentThread().isInterrupted()){  
  82.                     rectangleProgressBar.setProgress(iCount);  
  83.                     circleProgressBar.setProgress(iCount);  
  84.                 }  
  85.                 break;  
  86.             }  
  87.         }  
  88.     };  
  89. }  
package com.tutor.progress; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.widget.Button; import android.widget.ProgressBar; public class ProgressBarDemo extends Activity { private ProgressBar rectangleProgressBar,circleProgressBar; private Button mButton; protected static final int STOP = 0x10000; protected static final int NEXT = 0x10001; private int iCount = 0; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //findView By id rectangleProgressBar = (ProgressBar)findViewById(R.id.rectangleProgressBar); circleProgressBar = (ProgressBar)findViewById(R.id.circleProgressBar); mButton = (Button)findViewById(R.id.button); rectangleProgressBar.setIndeterminate(false); circleProgressBar.setIndeterminate(false); mButton.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { rectangleProgressBar.setVisibility(View.VISIBLE); circleProgressBar.setVisibility(View.VISIBLE); rectangleProgressBar.setMax(100); rectangleProgressBar.setProgress(0); circleProgressBar.setProgress(0); //创建一个线程,每秒步长为5增加,到100%时停止 Thread mThread = new Thread(new Runnable() { public void run() { for(int i=0 ; i < 20; i++){ try{ iCount = (i + 1) * 5; Thread.sleep(1000); if(i == 19){ Message msg = new Message(); msg.what = STOP; mHandler.sendMessage(msg); break; }else{ Message msg = new Message(); msg.what = NEXT; mHandler.sendMessage(msg); } }catch (Exception e) { e.printStackTrace(); } } } }); mThread.start(); } }); } //定义一个Handler private Handler mHandler = new Handler(){ public void handleMessage(Message msg){ switch (msg.what) { case STOP: rectangleProgressBar.setVisibility(View.GONE); circleProgressBar.setVisibility(View.GONE); Thread.currentThread().interrupt(); break; case NEXT: if(!Thread.currentThread().isInterrupted()){ rectangleProgressBar.setProgress(iCount); circleProgressBar.setProgress(iCount); } break; } } }; }

 

第四步:运行上述工程,效果如下图:

Android 基础教程之-------Android ProgressBar的使用!_第4张图片

 

OK,今天就先到这里,希望对大家有所帮助,大家多留言。谢谢大家 ~

原文地址:http://blog.csdn.net/Android_Tutor/article/details/5695170

你可能感兴趣的:(Android 基础教程之-------Android ProgressBar的使用!)