Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 控件開發:ProgressBar 進度條的完整用法

編輯:關於 Android 程式設計

ProgressBar 是回饋「事情正在進行中」最直接的控件。它既可以當作不確定進度的轉圈提示,也可以當作有明確百分比的橫向進度條。本文用一個可重複播放的範例,把水平進度條、次要進度、可見性切換這幾個常用 API 一次講清楚。

一、先建立介面

布局裡放了兩個 ProgressBar:一個使用 progressBarStyleHorizontal 樣式(橫向長條),一個使用預設樣式(圓形轉圈)。兩者初始都設為 gone,等使用者按下按鈕才顯示,這樣可以觀察 setVisibility 的效果。

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <ProgressBar
        android:id="@+id/firstBar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:max="200"
        android:visibility="gone" />

    <ProgressBar
        android:id="@+id/secendBar"
        style="?android:attr/progressBarStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone" />

    <Button
        android:id="@+id/myButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="button" />
</LinearLayout>

二、Activity 程式碼

package com.example.progress;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;

public class MainActivity extends Activity {

    private ProgressBar firstBar = null;
    private ProgressBar secendBar = null;
    private Button myButton = null;
    private int i = 0; // 記錄目前進度

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        firstBar = (ProgressBar) findViewById(R.id.firstBar);
        secendBar = (ProgressBar) findViewById(R.id.secendBar);
        myButton = (ProgressBar & View) findViewById(R.id.myButton);
        myButton = (Button) findViewById(R.id.myButton);

        myButton.setOnClickListener(new myButtonClickListener());
    }

    class myButtonClickListener implements OnClickListener {
        @Override
        public void onClick(View v) {
            if (i == 0) {
                // 第一次點擊:讓兩個進度條顯示出來
                firstBar.setVisibility(View.VISIBLE);
                secendBar.setVisibility(View.VISIBLE);
            } else if (i > 0 && i < firstBar.getMax()) {
                // 主進度與次要進度同步推進
                firstBar.setProgress(i);
                firstBar.setSecondaryProgress(i + 10);
            } else {
                // 走完一輪:隱藏並歸零,i 設為 -10 讓下一輪從 0 重新開始
                firstBar.setVisibility(View.GONE);
                secendBar.setVisibility(View.GONE);
                firstBar.setProgress(0);
                firstBar.setSecondaryProgress(0);
                i = -10;
            }
            i += 10;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

三、幾個重點 API

setMax / getMax:決定進度的上限。布局中的 android:max="200" 等同於 setMax(200),預設值是 100。

setProgress:設定主要進度,值必須落在 0 到 max 之間。

setSecondaryProgress:設定次要進度,常用於「緩存進度」這種場景——例如線上影片播放時,一條淺色條表示已緩衝多少,一條深色條表示已播放到哪裡。

setVisibility:注意 View.GONE 不佔排版空間,View.INVISIBLE 佔位但不可見。進度條通常用 GONE,避免畫面留下空白。

四、實務上的建議

如果只是要「不知道還要多久」的提示,直接用預設的圓形樣式並讓它持續轉就好,不必自己開 Timer。若是有明確進度(下載、安裝、上傳),則改用水平樣式並搭配百分比文字,使用者體驗會好很多。另外,長時間的耗時任務建議放在服務或背景執行緒中推進度,避免只靠按鈕點擊這種教學式寫法。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved