Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

Android ProgressBar 三種型態實作:對話框、標題列與水平進度列

編輯:Android 開發實例

Android 的進度提示可分為三大類:跳出式的對話框進度列、隱身在標題列的忙碌指示、以及可精確顯示百分比的水平進度列。三者適用情境不同,實作方式也各自獨立。

一、對話框進度列 ProgressDialog

建立步驟只有兩個:覆寫 Activity 的 onCreateDialog() 並在其中建立對話框,然後呼叫 showDialog() 把它顯示出來。

package com.amaker.test;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
    private Button myBtn;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        myBtn = (Button)findViewById(R.id.Button01);
        myBtn.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                showDialog(0);
            }
        });
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        ProgressDialog dialog = new ProgressDialog(this);
        // 也可以不顯示標題
        dialog.setTitle("測試對話框");
        dialog.setIndeterminate(true);
        dialog.setMessage("程式正在載入請稍後!");
        dialog.setCancelable(true);

        dialog.setButton(Dialog.BUTTON_POSITIVE, "確定",
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            }
        );
        return dialog;
    }
}

setIndeterminate(true) 代表「不知道還要多久」的轉圈模式;若想顯示百分比,改為 STYLE_HORIZONTAL 並用 setProgress() 更新即可。

二、標題列進度列

這種做法不跳出任何視窗,而是在標題列右側顯示一個小小的忙碌轉盤,適合干擾性要降到最低的場合。關鍵是必須在 setContentView() 之前先向 Window 申請這個功能。

package com.amaker.test;

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

public class MainActivity extends Activity {

    private Button b1, b2;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        setContentView(R.layout.main);
        b1 = (Button)findViewById(R.id.Button01);
        b2 = (Button)findViewById(R.id.Button02);

        b1.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                setProgressBarIndeterminateVisibility(true);
            }
        });

        b2.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                setProgressBarIndeterminateVisibility(false);
            }
        });
    }
}

三、水平進度列

這是唯一能呈現精確百分比的型態,步驟為:在版面配置中宣告 ProgressBar、在 Activity 中取得實例、再以 incrementProgressBy() 增量更新。

package com.amaker.test;

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

public class MainActivity extends Activity {
    private Button b1, b2;
    ProgressBar progressBar;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        b1 = (Button)findViewById(R.id.Button01);
        b2 = (Button)findViewById(R.id.Button02);
        progressBar = (ProgressBar)findViewById(R.id.ProgressBar01);

        b1.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                progressBar.incrementProgressBy(1);
            }
        });

        b2.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                progressBar.incrementProgressBy(-1);
            }
        });
    }
}

對應的版面配置如下。style 指定為 progressBarStyleHorizontal 是它呈現長條形的關鍵,android:max 定義上限、android:progress 定義初始值。

<?xml version="1.0" encoding="utf-8"?>
<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/ProgressBar01"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="200dip"
        android:layout_height="wrap_content"
        android:max="100"
        android:progress="50"
    ></ProgressBar>

    <Button
        android:id="@+id/Button01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="增加"></Button>

    <Button
        android:id="@+id/Button02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="減少"></Button>

</LinearLayout>

incrementProgressBy() 接受負值,因此同一個方法就能同時處理加與減。實務上若要更新進度,記得背景執行緒不能直接操作介面,需透過 Handler 或 runOnUiThread() 回到主執行緒。

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