編輯:關於 Android 程式設計
Dialog 是 Android 裡使用頻率極高的元件,但變體眾多、API 各異。本文用六個可運行的範例把常見形態全部走過一遍。
先用六個按鈕作為入口:
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" >
<Button android:id="@+id/plainDialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="普通 Dialog" />
<Button android:id="@+id/plainDialogEvent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Dialog 按鈕事件集中處理" />
<Button android:id="@+id/inputDialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="請輸入框" />
<Button android:id="@+id/listDialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="列表對話框" />
<Button android:id="@+id/radioDialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="單選對話框" />
<Button android:id="@+id/checkboxDialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="多選對話框" />
<Button android:id="@+id/diyDialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="自定義佈局對話框" />
</LinearLayout>
private void plainDialogDemo() {
Button plainBtn = (Button) findViewById(R.id.plainDialog);
plainBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
new AlertDialog.Builder(DialogActivity.this)
.setTitle("刪除")
.setMessage("確定刪除指定資料")
.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "確定了",
Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setCancelable(false).show();
}
});
}
這是最基礎的形態。注意 setCancelable(false) 的作用:使用者無法用返回鍵關閉,必須明確點選按鈕,適用在有破壞性後果的操作上。
private void inputDialog() {
Button inputBtn = (Button) findViewById(R.id.inputDialog);
inputBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
final EditText et = new EditText(DialogActivity.this);
new AlertDialog.Builder(DialogActivity.this)
.setTitle("請輸入數字")
.setView(et)
.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(),
et.getText(), Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("取消", null)
.setCancelable(false).show();
}
});
}
關鍵是 setView():把任意 View 塞進對話框內容區即可。這裡直接 new 了一個 EditText,實務上更常見的做法是傳入一個佈局資源 id,用 LayoutInflater 載入自訂的輸入表單。
private void listDialogDemo() {
Button listBtn = (Button) findViewById(R.id.listDialog);
listBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
final String[] names = { "C羅", "J羅", "H羅" };
new AlertDialog.Builder(DialogActivity.this).setTitle("列表對話框")
.setItems(names, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(DialogActivity.this, names[which],
Toast.LENGTH_SHORT).show();
}
}).setNegativeButton("取消", null).show();
}
});
}
setItems() 接受字串陣列,回呼中的 which 就是被點選項目的索引。
private void radioDialogDemo() {
Button radioButton = (Button) findViewById(R.id.radioDialog);
radioButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
final String[] names = { "C羅", "J羅", "H羅" };
new AlertDialog.Builder(DialogActivity.this)
.setTitle("列表對話框")
.setSingleChoiceItems(names, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
selecteName = names[which];
}
})
.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(DialogActivity.this, selecteName,
Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("取消", null).show();
}
});
}
setSingleChoiceItems() 的第二個參數是預設勾選的索引。注意選擇時機的設計:點選單項只是暫存到變數,真正「確認」要等使用者按確定——這與列表 Dialog 點一下立即生效的行為不同,是刻意的設計選擇。
private void checkDialogDemo() {
Button checkBtn = (Button) findViewById(R.id.checkboxDialog);
checkBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
final String[] names = { "C羅", "J羅", "H羅" };
final boolean[] selected = new boolean[] { true, false, true };
new AlertDialog.Builder(DialogActivity.this)
.setMultiChoiceItems(names, selected,
new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which, boolean isChecked) {
}
})
.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
StringBuilder sb = new StringBuilder("你選擇了:");
for (int i = 0; i < names.length; i++) {
if (selected[i]) {
sb.append(names[i]);
}
}
Toast.makeText(DialogActivity.this, sb.toString(),
Toast.LENGTH_SHORT).show();
}
}).setNegativeButton("取消", null).show();
}
});
}
這裡有個容易踩的坑:selected 陣列必須是final的,因為它會被匿名內部類捕獲;同時由於 setMultiChoiceItems 內部持有的是同一個陣列引用,勾選狀態會即時反映到這個陣列上,所以按確定時直接讀它就能拿到正確結果。
private void customDialogDemo() {
final AlertDialog dlg = new AlertDialog.Builder(this).create();
dlg.show();
Window window = dlg.getWindow();
window.setContentView(R.layout.diylayout);
ImageButton ok = (ImageButton) window.findViewById(R.id.btnok);
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "關閉了", Toast.LENGTH_SHORT).show();
dlg.dismiss();
}
});
}
自訂佈局檔:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/dialogimg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/dialog_bg" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/dialogimg"
android:layout_alignTop="@id/dialogimg"
android:layout_marginLeft="50dp"
android:layout_marginTop="60dp"
android:text="自定義的 dialog" />
<ImageButton
android:id="@+id/btnok"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignRight="@id/dialogimg"
android:layout_alignTop="@id/dialogimg"
android:layout_marginRight="15dp"
android:layout_marginTop="15dp"
android:background="@drawable/close_dialog" />
</RelativeLayout>
自訂的順序很關鍵:必須先 show(),再呼叫 getWindow() 並 setContentView()。原因是 Dialog 的 Window 在 show 之前尚未完成初始化。找到子控制項時要用 window.findViewById() 而非 Activity 的版本,否則會找不到。
能用系統現成的形態(列表、單選、多選)就不要自訂,一致性與無障礙支援都更好。只有當設計稿要求特殊視覺呈現時才走自定義佈局路線。另外別忘了:在 Activity 中直接 new Dialog 容易在螢幕旋轉時造成 Window 洩漏,正式專案建議改用 DialogFragment,把生命週期交給系統管理。
Android 透過 WebService 存取 SQL Server:資料庫、伺服器端到客戶端的完整實作
開頭先把兩件事講清楚,免得又被說是標題黨。 第一,Android 沒辦法直接連上 SQL Server。SQL Server 裝完動輒好幾個 GB,而 App 跑在記憶體
React Native 樣式表全解析:可用屬性清單與實際用法
前面寫了一系列 Android React Native 的文章,較為複雜的內容基本都梳理完了,這次換點輕鬆的話題——樣式。也就是我們熟悉的 CSS 樣式,那麼一個 Vi
Android 仿網易新聞客戶端分類拖拽排序功能實現
先對比一下網易新聞客戶端與自己實現的效果,當然原版的效果還是更細膩一些。 gridviewsort.gif 如何實現拖拽一個 Item 思路是:用 WindowMan
TextView 控件進階用法全解(下)
(一)帶陰影的 TextView 涉及到的幾個屬性: 運行效果如下: 實現代碼: (二)帶邊框的 TextView: 簡單說下 shapeDrawable 資