編輯:關於 Android 程式設計
日常開發中最常遇到的對話框有五種:確認框、列表框、多選框、單選框,以及完全自定義的對話框。它們都建立在同一組 Builder 方法之上。
· setTitle:設定標題
· setMessage:設定文字內容
· setIcon:設定圖標
· setItems:設定要顯示的列表
· setMultiChoiceItems:多選(複選框)列表
· setSingleChoiceItems:單選按鈕列表
· setNeutralButton:普通(中立)按鈕
· setPositiveButton:確定按鈕
· setNegativeButton:取消按鈕
· setView:載入自定義佈局

package com.example.alertdialog;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
private Button button1;
private Button button2;
private Button button3;
private Button button4;
private Button button5;
private int mark = 0;
private String[] item = {"學生", "工人", "教師", "農民"};
private String[] multChoice = {"旅游", "電影", "運動", "讀書"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.btn_button1);
button2 = (Button) findViewById(R.id.btn_button2);
button3 = (Button) findViewById(R.id.btn_button3);
button4 = (Button) findViewById(R.id.btn_button4);
button5 = (Button) findViewById(R.id.btn_button5);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
button4.setOnClickListener(this);
button5.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
// 1. 確認對話框
case R.id.btn_button1: {
AlertDialog.Builder builder = new Builder(this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("確認對話框");
builder.setMessage("確認退出?");
builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "你單擊了確定按鈕",
Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "你單擊了取消按鈕",
Toast.LENGTH_SHORT).show();
}
});
builder.create();
builder.show();
break;
}
// 2. 列表對話框
case R.id.btn_button2: {
AlertDialog.Builder builder = new Builder(this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("職業");
builder.setItems(item, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "你的職業是" + item[which],
Toast.LENGTH_SHORT).show();
}
});
builder.create();
builder.show();
break;
}
// 3. 多選對話框
case R.id.btn_button3: {
final boolean[] choose = new boolean[multChoice.length];
AlertDialog.Builder builder = new Builder(this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("愛好");
// 第二個參數傳 null 代表無預設勾選
builder.setMultiChoiceItems(multChoice, null,
new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which,
boolean isChecked) {
// 即時記錄每一項的勾選狀態
choose[which] = isChecked;
}
});
builder.setPositiveButton("確認", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 按下確認時才彙整結果
String result = "";
for (int i = 0; i < multChoice.length; i++) {
if (choose[i]) {
result += multChoice[i] + " ";
}
}
Toast.makeText(MainActivity.this, "你的愛好[" + result + "]",
Toast.LENGTH_SHORT).show();
}
});
builder.create();
builder.show();
break;
}
// 4. 單選對話框
case R.id.btn_button4: {
mark = 0;
AlertDialog.Builder builder = new Builder(this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("職業");
builder.setSingleChoiceItems(item, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mark = which;
}
});
builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "你的職業:" + item[mark],
Toast.LENGTH_SHORT).show();
}
});
builder.create();
builder.show();
break;
}
// 5. 自定義對話框
case R.id.btn_button5: {
LayoutInflater inflater = LayoutInflater.from(this);
View view = inflater.inflate(R.layout.item, null);
AlertDialog.Builder builder = new Builder(this);
builder.setTitle("自定義對話框");
builder.setView(view);
builder.setNeutralButton("普通按鈕", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "我是自定義的對話框哦",
Toast.LENGTH_SHORT).show();
}
});
builder.create();
builder.show();
break;
}
}
}
}
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="@+id/btn_button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="確認對話框" />
<Button
android:id="@+id/btn_button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="列表對話框" />
<Button
android:id="@+id/btn_button3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="多選對話框" />
<Button
android:id="@+id/btn_button4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="單選對話框" />
<Button
android:id="@+id/btn_button5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="自定義對話框" />
</LinearLayout>
</RelativeLayout>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitCenter"
android:src="@drawable/icon" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="我是自定義的對話框" />
</LinearLayout>
幾點實務提醒:多選與單選對話框都必須自行維護選擇狀態(範例中的 choose[] 與 mark),因為 OnClickListener 只是通知你「哪一項被點了」,不會幫你記住結果;而 setView 搭配自定義佈局,則是把任意複雜 UI 塞進對話框的通用手段。
Android 記憶體洩漏實戰排查(一):從 Gallery OOM 崩潰追到一個 static 變數
問題背景 負責 Gallery 模組測試時,回報了一個很惱人的現象:使用一段時間後必定 Crash,翻 log 發現是 OOM。麻煩的是測試團隊無法提供重現步驟,只知道「
Android 非同步任務載入資料的兩種介面實作
Android 非同步任務的本質,是在背景開啟一個執行緒完成耗時操作,而 onPreExecute 與 onPostExecute 等方法則運行於 UI 主執行緒,用來更
Android 圖形庫 Skia 入門:編譯並產生一張 PNG 圖
Skia 是 Google 開發的跨平台 2D 圖形庫,Android 的畫布繪製、字型渲染背後都是它。它同時也有 PC 版本,先在本機跑通最小範例,有助於理解整個渲染管
Android 消息機制深度解析:Handler、Looper 與 MessageQueue 原理
Android 的消息處理相關類別包括:Looper、Handler、MessageQueue、Message、ThreadLocal、ThreadLocal.Value