2015年3月1日 星期日

Android控制項ProgressBar

進度控制項-ProgressBar

ProgressBar有兩個重點

  1. 配置的型態,開發人員配置進度控制項要根據實際的狀況,跟使用習慣判斷哪種型態與外觀最適合。
  2. 背景或其他執行緒也進行著,利用ProgressBar來回饋最新進度給使用者。
sample_progress_bar.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="match_parent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="大圓形進度控制項"
            android:id="@+id/big_progressbar" />

        <ProgressBar
            style="?android:attr/progressBarStyleLarge"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/prb_big" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="中圓形進度控制項"
            android:id="@+id/textView3" />

        <ProgressBar
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/prb_center" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="小圓形進度控制項"
            android:id="@+id/textView4" />

        <ProgressBar
            style="?android:attr/progressBarStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/prb_little" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="水平進度控制項"
            android:id="@+id/textView5" />

        <ProgressBar
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/prb_horizontal"
            android:indeterminateOnly="false"
            android:minHeight="20dip"
            android:longClickable="false"
            android:max="100"
            android:progress="30"
            android:secondaryProgress="65" />
    </LinearLayout>
</LinearLayout>
SampleProgressBar.java
package com.example.win7.android_widget;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
/*使用Message來作為副執行緒與主執行緒兼傳遞訊息的物件,引用其package*/
import android.os.Message;
import android.widget.ProgressBar;

/**
 * Created by win7 on 2015/2/19.
 */
public class SampleProgressBar extends Activity {
    private ProgressBar prb_horizontal;
    int myProgress=0;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sample_progress_bar);
        setTitle("這是ProgressBar的Activity");
        prb_horizontal = (ProgressBar) findViewById(R.id.prb_horizontal);
        prb_horizontal.setProgress(myProgress);
        /*使用一個執行緒Thread來執行讓ProgressBar的進度增加的動作,因為不能讓主畫面當在那邊所以使用另一個執行緒來執行,這是ProgressBar的標準做法*/
        new Thread(new Runnable() {
            /*覆寫Runnable類別的run()方法,將要執行的程式碼放在裡面,當這個執行緒被執行就會直接執行run()方法*/
            public void run() {
                while (myProgress < 100) {
                    try {
                        myHandle.sendMessage(myHandle.obtainMessage());
                        Thread.sleep(100);
                    } catch (Throwable t) {

                    }
                }
             }
            /*Thread物件不用指派變數就直接乎叫start()方法讓執行緒開始執行*/
        }).start();

    }
        /*建立一個Handler實體來處理傳送過來的Message,每當接收到Message就把ProgressBar的進程加1*/
        Handler myHandle = new Handler() {
            public void handleMessage(Message msg){
                myProgress++;
                prb_horizontal.setProgress(myProgress);
            }
        };



}

沒有留言:

張貼留言