Android 程式開發:(十)基本控件 —— 10.3 ProgressBar

當執行某些正在處理的任務時,ProgressBar提供瞭一個可視化的反饋。例如,你在從web服務器下載數據,然後需要更新下載的狀態。在這種情況下,ProgressBar就是一個很好的選擇。下面的例子,展示如何去使用ProgressBar。

1、創建一個工程,BasicViews2。

2、main.xml中的代碼。

[html] 
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 
 
<ProgressBar android:id="@+id/progressbar" 
    android:layout_width="wrap_content"  
    android:layout_height="wrap_content" /> 
 
</LinearLayout> 
3、Basic2Activity.java中的代碼。
[java] 
package net.learn2develop.BasicViews2; 
 
import android.app.Activity; 
import android.os.Bundle; 
import android.os.Handler; 
import android.view.View; 
import android.widget.ProgressBar; 
 
public class BasicViews2Activity extends Activity { 
    static int progress; 
    ProgressBar progressBar; 
    int progressStatus = 0; 
    Handler handler = new Handler(); 
 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
         
        progress = 0; 
        progressBar = (ProgressBar) findViewById(R.id.progressbar); 
         
        //—do some work in background thread— 
        new Thread(new Runnable() 
        { 
            public void run() 
            { 
                //?do some work here? 
                while (progressStatus < 10) 
                { 
                    progressStatus = doSomeWork(); 
 
                } 
 
                //—hides the progress bar— 
                handler.post(new Runnable() 
                { 
                    public void run() 
                    { 
                        //—0 – VISIBLE; 4 – INVISIBLE; 8 – GONE— 
                        progressBar.setVisibility(View.GONE); 
                    } 
                }); 
            } 
 
            //—do some long running work here— 
            private int doSomeWork()  
            { 
                try { 
                    //—simulate doing some work— 
                    Thread.sleep(500); 
                } catch (InterruptedException e) 
                { 
                    e.printStackTrace(); 
                } 
                return ++progress; 
            } 
        }).start(); 
    } 

4、F11調試,會看見ProgressBar的動畫,5秒之後,動畫消失。

 

接下來展示如何自定義ProgressBar的樣式。
1、使用之前的例子,修改main.xml。
[html] 
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 
 
<ProgressBar android:id="@+id/progressbar" 
    android:layout_width="wrap_content"  
    android:layout_height="wrap_content" 
    style="@android:style/Widget.ProgressBar.Horizontal" /> 
 
</LinearLayout> 
2、Basic2Activity.java中的代碼。

[java] 
package net.learn2develop.BasicViews2; 
 
import android.app.Activity; 
import android.os.Bundle; 
import android.os.Handler; 
import android.view.View; 
import android.widget.ProgressBar; 
 
public class BasicViews2Activity extends Activity { 
    static int progress; 
    ProgressBar progressBar; 
    int progressStatus = 0; 
    Handler handler = new Handler(); 
 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
         
        progress = 0; 
        progressBar = (ProgressBar) findViewById(R.id.progressbar); 
        progressBar.setMax(200); 
         
        //—do some work in background thread— 
        new Thread(new Runnable() 
        { 
            public void run() 
            { 
                //?do some work here? 
                while (progressStatus < 100) 
                { 
                    progressStatus = doSomeWork(); 
 
                    //?Update the progress bar? 
                    handler.post(new Runnable() 
                    { 
                        public void run() { 
                            progressBar.setProgress(progressStatus); 
                        } 
                    }); 
                } 
 
                //—hides the progress bar— 
                handler.post(new Runnable() 
                { 
                    public void run() 
                    { 
                        //—0 – VISIBLE; 4 – INVISIBLE; 8 – GONE— 
                        progressBar.setVisibility(View.GONE); 
                    } 
                }); 
            } 
 www.aiwalls.com
            //—do some long running work here— 
            private int doSomeWork()  
            { 
                try { 
                    //—simulate doing some work— 
                    Thread.sleep(50); 
                } catch (InterruptedException e) 
                { 
                    e.printStackTrace(); 
                } 
                return ++progress; 
            } 
        }).start(); 
    } 

3、F11調試。
4、當ProgressBar的進度到達50%的時候,它就會消失。

 

附:

ProgressBar的樣式列表。

Widget.ProgressBar.Horizontal
Widget.ProgressBar.Small
Widget.ProgressBar.Large
Widget.ProgressBar.Inverse
Widget.ProgressBar.Small.Inverse
Widget.ProgressBar.Large.Inverse

發佈留言