編輯:Android 開發實例
對話框是一個浮在目前 Activity 之前的小視窗。它出現時,背後的 Activity 失去焦點,所有使用者互動都由對話框接收,因此適合承載通知、確認與短暫的操作流程。
若以上都不符合需求,可以從 Dialog 類別或上述子類別擴充,並定義全新的版面配置。
對話框通常是 Activity 的一部分。建議在 Activity 的 onCreateDialog(int) 回呼中建立它,這樣 Android 會自動把該 Activity 設為對話框的擁有者,進而代管每個對話框的狀態並掛靠到 Activity 上。掛靠的好處是對話框會繼承 Activity 的屬性行為:例如對話框開啟時按菜單鍵仍會顯示該 Activity 定義的選項菜單,音量鍵也會調整該 Activity 使用的音效流。
顯示時呼叫 showDialog(int),並傳入一個唯一識別該對話框的整數 ID。第一次請求時,系統會呼叫 onCreateDialog(int) 讓你初始化並回傳 Dialog 物件。
在對話框真正顯示前,系統還會呼叫可選的 onPrepareDialog(int, Dialog)。差別在於:onCreateDialog() 只在該對話框第一次開啟時呼叫,而 onPrepareDialog() 每次開啟都會呼叫。因此若你希望每次開啟時更新內容(例如顯示最新的檔案清單),就應該寫在 onPrepareDialog() 裡;若不實作它,對話框會保持上次開啟時的樣子。
實作這兩個回呼時,最佳做法是用 switch 依傳入的 id 分派。以下以一個遊戲的「暫停」與「結束」兩個對話框為例:
static final int DIALOG_PAUSED_ID = 0;
static final int DIALOG_GAMEOVER_ID = 1;
protected Dialog onCreateDialog(int id) {
Dialog dialog;
switch(id) {
case DIALOG_PAUSED_ID:
// 定義暫停對話框
break;
case DIALOG_GAMEOVER_ID:
// 定義遊戲結束對話框
break;
default:
dialog = null;
}
return dialog;
}
要顯示其中一個時,帶著 ID 呼叫即可:
showDialog(DIALOG_PAUSED_ID);
補充一點:若你選擇在 onCreateDialog() 之外建立對話框,它不會自動掛靠到 Activity,此時可用 setOwnerActivity(Activity) 手動指定擁有者。
關閉方式有三種,語意各不相同:
若想在對話框消失時執行某段流程,實作 DialogInterface.OnDismissListener 介面(只有一個方法 onDismiss(DialogInterface)),再透過 setOnDismissListener() 註冊。
但要特別區分「消除」與「取消」:當使用者按返回鍵,或程式呼叫 cancel()(例如按下對話框上的取消鈕)時,屬於「取消」。取消時 OnDismissListener 依然會收到通知;若你只想在取消時被通知,就應該改用 setOnCancelListener() 註冊 DialogInterface.OnCancelListener。
AlertDialog 具備標題、文字訊息、1~3 個按鈕、以及可選的項目清單(可為核取方塊或單選鈕),幾乎涵蓋所有對話框需求。做法是透過 AlertDialog.Builder 子類別串起各項設定,最後呼叫 create() 取得物件。
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
MyActivity.this.finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
先用 setMessage(CharSequence) 加上訊息,再以 setCancelable(false) 讓使用者無法用返回鍵關閉,最後為每個按鈕呼叫對應的 set...Button() 方法,並傳入按鈕文字與 OnClickListener。
這裡有個限制常被誤解:三種按鈕(positive、neutral、negative)各自只能有一個,因此總數最多三個,且不能出現兩個「確定」。這些名稱與按鈕的實際語意無關,只是用來幫助你記錄位置。
final CharSequence[] items = {"Red", "Green", "Blue"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Pick a color");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
}
});
AlertDialog alert = builder.create();
setItems() 接受一組要顯示的項目與一個 OnClickListener,回呼中的 item 參數就是被選中的索引。
多選清單用 setMultiChoiceItems(),單選清單用 setSingleChoiceItems()。若在 onCreateDialog() 中建立,Android 會代管清單狀態:只要 Activity 仍存活,對話框會記住上次的勾選;使用者離開 Activity 後選擇則會遺失。若要跨 Activity 生命週期保存,必須自行在 onSaveInstanceState() 等環節處理;若要永久保存,則需搭配資料儲存機制(如 SharedPreferences)。
final CharSequence[] items = {"Red", "Green", "Blue"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Pick a color");
builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
}
});
AlertDialog alert = builder.create();
setSingleChoiceItems() 的第二個參數是預設勾選項的索引(從 0 起算),傳入 -1 代表不預設勾選任何項目。
ProgressDialog 是 AlertDialog 的擴充,可為「不知道要多久」的任務顯示旋轉輪,也可為「已知進度」的任務顯示進度列,並同樣能帶按鈕(例如取消下載)。最簡單的寫法是直接呼叫靜態方法:
ProgressDialog dialog = ProgressDialog.show(MyActivity.this, "", "Loading. Please wait...", true);
四個參數依序是:Context、標題(此處留空)、訊息文字,以及是否為不確定進度(此參數僅與進度列有關)。這種寫法不必經過 onCreateDialog() 管理。
步驟如下:
ProgressDialog progressDialog;
progressDialog = new ProgressDialog(mContext);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMessage("Loading...");
progressDialog.setCancelable(false);
設定本身很簡單,真正需要留意的是更新邏輯:耗時工作應該放在另一個執行緒,進度再透過 Handler 等方式回傳給 UI 執行緒更新,否則在長時間運算中畫面會凍住。
package com.amaker.test;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
/**
* AlertDialog 測試
*/
public class MainActivity extends Activity {
private TextView myTV;
private Button myBtn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myTV = (TextView)findViewById(R.id.TextView01);
myBtn = (Button)findViewById(R.id.Button01);
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
myBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
builder.setMessage("真的要刪除該記錄嗎?").setPositiveButton("是", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
myTV.setText("刪除成功!");
}
}).setNegativeButton("否", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
myTV.setText("取消刪除!");
}
});
AlertDialog ad = builder.create();
ad.show();
}
});
}
}
package com.amaker.test;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView myTV;
private Button myBtn;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myTV = (TextView)findViewById(R.id.TextView01);
myBtn = (Button)findViewById(R.id.Button01);
final String[] items = {"奧爾良雞腿堡", "麻辣雞腿堡", "咖啡"};
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
myBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// 改為 setItems 則為一般清單樣式
builder.setTitle("請點餐").setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
myTV.setText(items[which]);
}
});
AlertDialog ad = builder.create();
ad.show();
}
});
}
}
把 setSingleChoiceItems() 換成 setItems(),就從「單選清單」變成「點選即關閉的一般清單」,兩者共用同一套 Builder 結構——這正是 Builder 模式在對話框上的價值:以流暢的鏈式呼叫組出各種型態。
Android 入門:用 TabHost 與 TabWidget 做分頁標籤
Tab 是行動介面裡最直覺的分頁方式,能把大量內容收進同一個螢幕,讓使用者左右切換而不必層層進入。代價是開發複雜度上升,所以建議真的有必要時再採用。 Android 的
Android 手機衛士(六):打包簽名並將 apk 部署到伺服器
本文記錄將專案打包生成 apk 並部署到伺服器的完整過程。整個流程可分為兩大步驟:1. 生成簽名檔,並指定其存放位置。2. 使用該簽名檔,為專案打包生成 apk。第一步:
Android 多媒體播放:MediaPlayer 控制與進度條同步
MediaPlayer 是 Android 中常用的多媒體播放類別。它可以播放本機音檔、網路串流與影片資源。若要播放放在 res/raw 的音檔,可以使用: Media
Android MediaPlayer 範例:播放控制、快進快退與背景播放
MediaPlayer 可用來播放 Android 中的音訊與影片。開發者可以透過 create() 建立播放器,再使用 start()、pause() 與 seekTo