編輯:關於 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 的鏈式調用,絕大多數交互場景都能游刃有餘。
打造 QQ 風格聊天與登入介面:記住密碼、氣泡與標題欄實作
前言本文以 QQ 風格聊天介面為靈感,示範如何製作登入頁、聊天頁與自訂標題欄。重點不只是畫面,還包括記住密碼、訊息列表更新、左右氣泡切換與返回操作。登入頁與記住密碼登入頁
Android 高級 UI 組件全解析:適配器、ListView、GridView、Spinner 與 ViewPager
在 Android 的界面開發裡,凡是需要「大量同構數據 + 可滾動展示」的場景,幾乎都離不開適配器(Adapter)這套設計。本文將帶你系統性地梳理適配器的核心概念,並
Android 開發筆記(一百一十一):用 Socket 打造聊天室
Socket 通信 基本概念 對程式開發來說,網路通信的地基就是 Socket。但正因為它是地基,用起來反而不容易上手。今天就來把 Socket 通信談清楚。 計算機網路
Android 開發之使用 Web Service 進行網絡編程
使用 Web Service 進行網絡編程 Android 應用通常運行在手機平台上,而手機的硬件資源(無論存儲還是計算能力)都是有限的。在 Android 上開發、運行