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

Android 學習筆記:ProgressDialog 的使用

編輯:關於 Android 程式設計

在許多電腦軟體或手機應用中,我們常會看到「載入中…」這類提示對話框,Android 應用自然也不例外。若想在自己的 App 中實現這樣的效果,就得用上 ProgressDialog。以下先來認識這個類別。

ProgressDialog 概覽

ProgressDialog 類別繼承自 AlertDialog,同樣存放於 android.app 套件中。它有兩種呈現形式:一種是圓圈旋轉式,另一種是水平進度條式,可透過以下兩個屬性值來設定:

static int STYLE_HORIZONTAL
    建立帶有水平進度條的 ProgressDialog。
static int STYLE_SPINNER
    建立帶有圓形旋轉進度條的 ProgressDialog。

請注意,當設定為水平進度條形式時,進度的取值範圍為 0—10000。

ProgressDialog 的建構方法有兩種:

ProgressDialog(Context context)
ProgressDialog(Context context, int theme)

除了建構方法外,ProgressDialog 也提供了以下靜態方法來回傳 ProgressDialog 物件:

static ProgressDialog show(Context context, CharSequence title, CharSequence message)
static ProgressDialog show(Context context, CharSequence title, CharSequence message, boolean indeterminate)
static ProgressDialog show(Context context, CharSequence title, CharSequence message, boolean indeterminate, boolean cancelable)
static ProgressDialog show(Context context, CharSequence title, CharSequence message, boolean indeterminate, boolean cancelable, DialogInterface.OnCancelListener cancelListener)

需要注意的是,第一個參數必須是目前運行中的 Activity 的 Context。

使用時的關鍵注意事項

Android 的 ProgressDialog 必須在背景程式運行完畢前,以 dismiss() 方法關閉已取得焦點的對話框,否則程式就會陷入無法終止的無窮迴圈。此外,在執行緒中不得變更 Context 或 parent View 的任何狀態、進行文字輸出等操作,因為執行緒裡的 Context 與 View 並不屬於 parent,兩者之間也沒有關聯。

範例程式

以下用一個簡單的程式來示範 ProgressDialog 的應用——點擊按鈕後彈出進度對話框,模擬背景執行 3 秒後再關閉:

public class MainActivity extends Activity
{
   private Button button=null;
   public ProgressDialog dialog=null;
   @Override
   protected void onCreate(Bundle savedInstanceState)
   {
       super.onCreate(savedInstanceState);
       super.setContentView(R.layout.activity_main);
       this.button=(Button)super.findViewById(R.id.button);
       this.button.setOnClickListener(new OnClickListener()
       {
           @Override
           public void onClick(View v)
           {
               final CharSequence strDialogTitle=MainActivity.this.getString(R.string.str_dialog_title);
               final CharSequence strDialogBody=MainActivity.this.getString(R.string.str_dialog_body);
               //顯示 Progress 對話框
               dialog=ProgressDialog.show(MainActivity.this,strDialogTitle,strDialogBody,true);
               new Thread()
               {
                   @Override
                   public void run()
                   {
                      try
                      {
                          //以暫停 3 秒模擬背景運行的程式碼
                          sleep(3000);
                      }
                      catch (InterruptedException e)
                      {
                          e.printStackTrace();
                      }
                      finally
                      {
                          //卸載 dialog 物件
                          dialog.dismiss();
                      }
                   }
                  .start();
               }
           });
   }
}

此程式的版面配置只需一個 Button 元件(id 為 button)即可。為了讓程式碼更貼近規範,範例在 strings.xml 中定義了字串資源:

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <string name="app_name">demo2</string>
   <string name="action_settings">Settings</string>
   <string name="hello_world">Hello world!</string>
   <string name="execute">執行</string>
   <string name="str_dialog_title">請稍等片刻</string>
   <string name="str_dialog_body">正在執行...</string>
</resources>

程式運行後便會看到進度提示對話框:

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