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

Android UI 開發:AlertDialog 彈窗控件的六種常見用法

編輯:關於 Android 程式設計

在日常使用手機的過程中,彈窗幾乎無處不在。無論是刪除一張照片、卸載一個應用,還是設定系統的日期與時間,背後往往都是同一類控件在支撐——它就是 AlertDialog。本文將以一個完整的小例子,帶大家梳理開發中最常打交道的幾種彈窗形態,並附上可直接運行的代碼。

一、界面佈局與數據準備

先在 activity_main.xml 中放置六個按鈕,分別對應列表、單選、多選、日期、時間與進度條六種彈窗,全部共用同一個點擊事件。

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <Button android:id="@+id/btn1" android:onClick="onClick" android:text="列表彈窗" .../>
    <Button android:id="@+id/btn2" android:onClick="onClick" android:text="單選彈窗" .../>
    <Button android:id="@+id/btn3" android:onClick="onClick" android:text="多選彈窗" .../>
    <Button android:id="@+id/btn4" android:onClick="onClick" android:text="日期彈窗" .../>
    <Button android:id="@+id/btn5" android:onClick="onClick" android:text="時間彈窗" .../>
    <Button android:id="@+id/btn6" android:onClick="onClick" android:text="進度條彈窗" .../>
</LinearLayout>

彈窗裡要展示的列表數據,用一個 string-array 統一管理會更清爽:

<string-array name="list">
    <item>列表一</item>
    <item>列表二</item>
    <item>列表三</item>
    <item>列表四</item>
    <item>列表五</item>
    <item>列表六</item>
</string-array>

二、核心邏輯

MainActivity 通過 findViewById 取得六個按鈕,統一實現 OnClickListener,再根據被點擊控件的 id 分派到各自的創建方法。

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.btn1: createListDialog(); break;
        case R.id.btn2: createSingleDialog(); break;
        case R.id.btn3: createMutilDialog(); break;
        case R.id.btn4: createDateDialog(); break;
        case R.id.btn5: createTimeDialog(); break;
        case R.id.btn6: createProgressBarDialog(); break;
    }
}

列表彈窗

借助 setItems 傳入 string-array 資源與監聽器,點擊某一項時 can 拿到它在列表中的位置 which。

new AlertDialog.Builder(this)
    .setTitle("列表彈窗")
    .setItems(R.array.list, (dialog, which) ->
        Toast.makeText(this, "點擊了第 " + which + " 項", Toast.LENGTH_SHORT).show())
    .setCancelable(false) // 點擊外部區域或返回鍵都不消失
    .show();

單選彈窗

setSingleChoiceItems 的第二個參數用來指定默認選中的索引(從 0 開始)。

new AlertDialog.Builder(this)
    .setTitle("單選彈窗")
    .setIcon(R.mipmap.ic_launcher)
    .setSingleChoiceItems(R.array.list, 1, (dialog, which) ->
        Toast.makeText(this, "當前選中第 " + which + " 項", Toast.LENGTH_SHORT).show())
    .setNegativeButton("取消", (d, w) -> d.dismiss())
    .setPositiveButton("確定", null)
    .setCancelable(false)
    .show();

多選彈窗

setMultiChoiceItems 需要一個 boolean 數組來標記初始勾選狀態。數組長度不能小於數據源,否則會越界;寫成 null 則代表默認全部未選中。

new AlertDialog.Builder(this)
    .setTitle("多選彈窗")
    .setIcon(R.mipmap.ic_launcher)
    .setMultiChoiceItems(R.array.list,
        new boolean[]{true, false, false, true, false, false},
        (dialog, which, isChecked) ->
            Toast.makeText(this, "第 " + which + " 項 勾選=" + isChecked, Toast.LENGTH_SHORT).show())
    .setNegativeButton("取消", (d, w) -> d.dismiss())
    .setPositiveButton("確定", null)
    .setCancelable(false)
    .show();

日期與時間彈窗

DatePickerDialog 與 TimePickerDialog 需要注意一個易錯點:月份是從 0 開始計數的,0 代表一月、11 代表十二月。

new DatePickerDialog(this, (view, year, month, day) ->
    Toast.makeText(this, year + "-" + (month + 1) + "-" + day, Toast.LENGTH_SHORT).show(),
    2016, 7, 15).show();

new TimePickerDialog(this, (view, hour, minute) ->
    Toast.makeText(this, hour + ":" + minute, Toast.LENGTH_SHORT).show(),
    11, 22, true).show();

進度條彈窗

ProgressDialog 可設置為水平樣式並指定最大進度。若調用 setCancelable(false),用戶將無法通過返回鍵或點擊外部關閉它,這種做法對體驗並不友好,一般只應用於強制升級等特殊場景。

ProgressDialog dialog = new ProgressDialog(this);
dialog.setTitle("進度條彈窗");
dialog.setMessage("正在加載...");
dialog.setMax(100);
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setProgress(50);
dialog.show();

三、效果預覽

列表彈窗:

單選彈窗:

多選彈窗:

日期彈窗:

時間彈窗:

進度條彈窗:

以上就是最常見的幾種彈窗形態。至於 PopupWindow,由於用法差異較大,這裡暫不展開。掌握好 AlertDialog.Builder 的鏈式調用,絕大多數交互場景都能游刃有餘。

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