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

Android 兩種進度條效果實作

編輯:關於 Android 程式設計

Android 兩種進度條效果實作

本例以點擊按鈕模擬下載進度,展示橫向進度條與環形(次要)進度條兩種效果,下載完成後進度條隱藏。

版面配置

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ProgressBar
        android:id="@+id/firstBar"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:visibility="gone" />
    <ProgressBar
        android:id="@+id/secondBar"
        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="begin" />
</LinearLayout>

Activity

public class ProgressBarTest extends Activity {
    private ProgressBar firstBar = null;
    private ProgressBar secondBar = null;
    private Button myButton = null;
    private int i = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        firstBar = (ProgressBar) findViewById(R.id.firstBar);
        secondBar = (ProgressBar) findViewById(R.id.secondBar);
        myButton = (Button) findViewById(R.id.myButton);
        myButton.setOnClickListener(new ButtonListener());
    }

    class ButtonListener implements OnClickListener {
        @Override
        public void onClick(View v) {
            if (i == 0) {
                firstBar.setVisibility(View.VISIBLE);
                firstBar.setMax(150);   // 手動設定最大值,預設為 100
                secondBar.setVisibility(View.VISIBLE);
            } else if (i < firstBar.getMax()) {
                firstBar.setProgress(i);
                firstBar.setSecondaryProgress(i + 10);
            } else {
                firstBar.setVisibility(View.GONE);
                secondBar.setVisibility(View.GONE);
            }
            i = i + 10;
        }
    }
}

firstBar 為帶進度的橫向進度條,可透過 setProgress 與 setSecondaryProgress 呈現主/次進度;secondBar 為預設的環形忙碌樣式。

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