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

Android 自訂 ProgressDialog 進度對話框完整用法

編輯:關於 Android 程式設計

系統內建的對話框樣式單一,很多時候需要呈現自訂的圖示、文字與進度型態。ProgressDialog 就是為此而生:它讓你決定對話框裡到底要顯示什麼。

常用方法速查

  • setProgressStyle:設定進度條風格,例如圓形旋轉。
  • setTitle:設定標題。
  • setMessage:設定提示訊息。
  • setIcon:設定標題圖示。
  • setIndeterminate:設定進度是否「不確定」。這個屬性對預設的轉輪模式沒有實際意義,只有搭配 ProgressBar 的對話框才有用;設為 false 後才能即時更新進度。
  • setCancelable:是否能用返回鍵取消。
  • CancelListener:對話框被強制取消後觸發,通常用來清理未完成的工作。
  • setButton:替對話框加上按鈕(記得要掛監聽器)。
  • show / cancel / dismiss:顯示、移除對話框;cancel 與 dismiss 效果相同。
  • setProgress(int):更新進度,實務上多搭配 Handler 或子執行緒使用。

實作程式碼

package cn.android;

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

public class SecondActivity extends Activity implements Runnable {

    private Button b_dialog, b_dialog1;
    private ProgressDialog pd, pd1;
    private int count;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);

        b_dialog  = (Button) this.findViewById(R.id.button_dialog);
        b_dialog1 = (Button) this.findViewById(R.id.Button_dialog1);

        b_dialog.setOnClickListener(listener);
        b_dialog1.setOnClickListener(listener);
    }

    private OnClickListener listener = new OnClickListener() {
        public void onClick(View v) {
            int id = v.getId();

            if (id == R.id.button_dialog) {
                // 一行建立並顯示,1 秒後由背景執行緒自行關閉
                pd = ProgressDialog.show(SecondActivity.this,
                        "自動關閉對話框", "Working,,,,,,1秒", true, false);
                Thread thread = new Thread(SecondActivity.this);
                thread.start();
            } else if (id == R.id.Button_dialog1) {
                pd1 = new ProgressDialog(SecondActivity.this);
                pd1.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                pd1.setTitle("提示");
                pd1.setMessage("這是一個條狀進度條對話框");
                pd1.setIcon(R.drawable.secondback);
                pd1.setIndeterminate(false);
                pd1.setCancelable(true);
                pd1.setProgress(100);
                pd1.show();

                count = 0;
                new Thread() {
                    public void run() {
                        try {
                            while (count <= 100) {
                                pd1.setProgress(count++);
                                Thread.sleep(100);
                            }
                            pd1.cancel();
                        } catch (Exception e) {
                            pd1.cancel();
                        }
                    }
                }.start();
            }
        }
    };

    // Runnable:模擬耗時作業
    public void run() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        handler.sendEmptyMessage(0);
    }

    // 收到訊息後關閉對話框並提示
    private Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            pd.dismiss();
            Toast.makeText(SecondActivity.this, "對話框就消失了", 3).show();
        }
    };

    // 按鈕監聽器
    class Bt1DialogListener implements DialogInterface.OnClickListener {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    }
}

實務提醒

以上寫法在單純的教學範例中很夠用,但在真實專案裡請注意兩點:一是 Android 新版不建議在背景執行緒直接操作 UI,更新進度應透過 Handler、runOnUiThread 或 LiveData 回主執行緒;二是 ProgressDialog 早已被官方標示為不建議使用,若只是要提示「忙碌中」,改用 ProgressBar 搭配自訂 DialogFragment 會更穩定,也能避免螢幕旋轉時的記憶體與視窗洩漏問題。

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