編輯:關於 Android 開發
項目裡的提示框往往長得都差不多,如果每處都重新寫一遍布局和樣式,維護成本很高。更好的做法是封裝一個通用 Dialog,把「標題、內容、兩個按鈕」做成可配置項,哪裡需要就用哪裡。
先看使用效果,一行聲明加幾行配置就能彈出來:
YouMengDialog userMsgDialog = null;
private void initTipDialog() {
userMsgDialog = new YouMengDialog(this);
userMsgDialog.setTitle("系統提示!");
userMsgDialog.setMessage("提示的內容!");
userMsgDialog.setCanceledOnTouchOutside(false);
userMsgDialog.setButton1("取消", new View.OnClickListener() {
@Override
public void onClick(View v) {
userMsgDialog.dismiss();
}
});
userMsgDialog.setButton2("確定", new View.OnClickListener() {
@Override
public void onClick(View v) {
userMsgDialog.dismiss();
// 在這裡處理確定後的業務邏輯
}
});
userMsgDialog.show();
}
setCanceledOnTouchOutside(false) 的作用值得強調:它禁止用戶點擊彈窗外部區域來關閉對話框。對於「刪除確認」這類不可撤銷的操作,這個設置能有效防止誤觸。
public class YouMengDialog extends Dialog {
private View contentView;
public YouMengDialog(Context context) {
super(context, R.style.tran_AlertDialog);
LayoutInflater inflater = LayoutInflater.from(context);
this.contentView = inflater.inflate(R.layout.dialog, null);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(contentView);
Window window = this.getWindow();
WindowManager.LayoutParams lp = window.getAttributes();
Display d = window.getWindowManager().getDefaultDisplay();
// 讓對話框寬度為屏幕的 90%
lp.width = (int) (d.getWidth() * 0.9F);
window.setAttributes(lp);
}
// 設置提示框標題
public void setTitle(String title) {
final TextView textView = (TextView) contentView.findViewById(R.id.title);
textView.setText(title);
}
// 設置提示的內容
public void setMessage(String message) {
final TextView textView = (TextView) contentView.findViewById(R.id.message);
textView.setText(message);
}
// 設置第一個按鍵(取消按鈕)
// title:按鈕上的文字;onClickListener:觸發的事件
public void setButton(String title, View.OnClickListener onClickListener) {
final Button button = (Button) contentView.findViewById(R.id.dialogButton);
button.setText(title);
button.setOnClickListener(onClickListener);
}
// 當只需要一個確定鍵時,讓取消鍵隱藏
public void setButtonGone() {
Button button = (Button) contentView.findViewById(R.id.dialogButton);
button.setVisibility(View.GONE);
}
// 設置第二個按鍵(確定按鈕)
public void setButton2(String title, View.OnClickListener onClickListener) {
final Button button = (Button) contentView.findViewById(R.id.dialogButton2);
button.setText(title);
button.setOnClickListener(onClickListener);
}
}
幾個設計要點:在構造函數裡完成 inflate,而不是在 onCreate 裡,這樣各個 setter 方法都能安全地操作到已存在的 View;在 onCreate 裡統一設置寬度,保證不同屏幕下比例一致;提供 setButtonGone() 方法,方便在只需要單按鈕的場景下隱藏取消鍵。
去掉系統默認對話框的邊框和標題,是讓自定義布局完全接管外觀的關鍵:
<style name="tran_AlertDialog" parent="@android:style/Theme.Dialog">
<item name="android:windowFrame">@null</item>
<!-- 邊框 -->
<item name="android:windowIsFloating">true</item>
<!-- 是否浮現在 activity 之上 -->
<item name="android:windowIsTranslucent">true</item>
<!-- 半透明 -->
<item name="android:windowNoTitle">true</item>
<!-- 無標題 -->
<item name="android:windowBackground">@drawable/gray_corners_border</item>
<!-- 自定義背景 -->
<item name="android:backgroundDimEnabled">true</item>
<!-- 背景模糊變暗 -->
</style>
其中 windowFrame 設為 @null 去掉系統邊框、windowNoTitle 去掉系統標題、windowBackground 換成自帶圓角的 nine-patch 或 shape 背景,這三項是「換皮」的核心。如果想自定義進出動畫,再補上 android:windowAnimationStyle 即可。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- 標題欄 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="@drawable/gray_bottom_border"
android:padding="10dp"
android:gravity="center_vertical">
<ImageView
android:src="@drawable/jinggao"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:maxWidth="25dp"
android:maxHeight="25dp"
android:scaleType="fitCenter"
android:layout_marginRight="8dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="提示信息"
android:id="@+id/title"
android:textColor="@color/black"
android:textSize="@dimen/txt_20" />
</LinearLayout>
<!-- 內容區 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="提示信息"
android:id="@+id/message"
android:textColor="@color/black"
android:textSize="@dimen/txt_20" />
</LinearLayout>
<!-- 按鈕區 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="10dp"
android:gravity="center">
<Button
android:layout_width="wrap_content"
android:layout_height="40dp"
android:text="取消"
android:background="@drawable/gray_corners_button"
android:id="@+id/dialogButton"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:textSize="@dimen/txt_20"
android:layout_weight="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="40dp"
android:text="確認"
android:background="@drawable/green_corners_button"
android:id="@+id/dialogButton2"
android:textSize="@dimen/txt_20"
android:textColor="@color/white"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
兩個按鈕都加上 android:layout_weight="1",就能自動等分寬度,不管文案長短都不會錯位。ID 命名上注意與 Java 中的 setButton / setButton2 保持對應:dialogButton 是左側取消鍵,dialogButton2 是右側確認鍵。
如果這個 Dialog 需要在多個 module 甚至多個 App 之間複用,建議再補充三點:一是把標題、消息的字號與顏色抽成自定義屬性(declare-styleable),讓調用方可以按需覆蓋;二是把寬度比例做成可配置項,因為提示框、列表選擇框、表單輸入框的最佳寬度往往不同;三是考慮改用 DialogFragment,它能被 FragmentManager 托管,在屏幕旋轉時自動重建,避免直接 new Dialog 帶來的內存洩漏風險。
Android DrawerLayout 側滑選單實作指南
Android DrawerLayout 側滑選單實作指南 DrawerLayout 是 Android Support Library 提供的側滑選單容器,常用於建立漢
Graphical Layout 預覽一片空白?三個方向逐一排查
解決 Android Graphical Layout 預覽不顯示的問題 在 Eclipse/ADT 時代,最常見的崩潰場景莫過於打開布局檔,右側的 Graphical
挖掘藍牙隱藏 API:用反射實作配對與解除配對
上一篇講了 Android 藍牙的基本用法,這次我們再往下挖一層,看看藍牙相關的隱藏 API。用過 Android 系統設定的人都知道,藍牙搜尋到裝置後可以進行「配對」與
Android AlarmManager 實戰:打造可重複觸發的定時鬧鐘
Android AlarmManager 實戰:打造可重複觸發的定時鬧鐘 幾乎每部手機都自帶鬧鐘。要在 Android 上實現類似能力,AlarmManager 是最直接