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

打造專屬客製化 Dialog:Builder 模式搭配工具類的完整實作

編輯:關於 Android 程式設計

做 Android 開發時,你一定遇過這個困擾:系統低版本的 Dialog 樣式老氣又醜,高版本雖然美觀一些,卻仍與公司 UI 設計師提供的效果圖有段距離。既然預設樣式滿足不了需求,那乾脆自己動手,讓 Dialog 的版面配置也能完全客製化。

先來看看最終呈現的效果:

是不是相當有質感?接下來就一步一步拆解實作流程。

一、自訂的 Dialog 類別

首先,我們先建立一個可自訂的 Dialog 類別,並透過內部 Builder 類別來集中管理所有設定:

 /** * 自定義帶兩個button的dialog * * @author Haipeng * * 2016-7-28 */ public class DefindedDialog extends Dialog { public DefindedDialog(Context context, int theme) { super(context, theme); } public DefindedDialog(Context context) { super(context); } public static class Builder { private Context context; private String title; private String message; private String positiveButtonText; private String negativeButtonText; private OnDedindedClickedListener onDedindedClickedListener; /** * * @param context * @param title * 標題 * @param message * 提示內容 * @param positiveButtonText * 確定按鈕的文本 * @param negativeButtonText * 取消按鈕 * @param onDedindedClickedListener * 兩個按鈕的監聽 */ public Builder(Context context, String title, String message, String positiveButtonText, String negativeButtonText, OnDedindedClickedListener onDedindedClickedListener) { super(); this.context = context; this.title = title; this.message = message; this.positiveButtonText = positiveButtonText; this.negativeButtonText = negativeButtonText; this.onDedindedClickedListener = onDedindedClickedListener; } /** * Create the custom dialog */ public DefindedDialog create() { final DefindedDialog dialog = new DefindedDialog(context, R.style.TwoButtonDialog);// 設置dialog的風格 // 設置dialog不可取消 點擊其他地方不能隱藏 dialog.setCanceledOnTouchOutside(false); dialog.setCancelable(false); View layout = View.inflate(context, R.layout.two_button_dialog, null); // 設置內容 TextView tv_content = (TextView) layout .findViewById(R.id.two_btn_desc); if (TextUtils.isEmpty(message)) { tv_content.setVisibility(View.GONE); } else { tv_content.setVisibility(View.VISIBLE); tv_content.setText(message); tv_content.setMovementMethod(new ScrollingMovementMethod()); } // 設置標題 TextView tv_title = (TextView) layout .findViewById(R.id.two_btn_title); // View view = layout.findViewById(R.id.view); if (TextUtils.isEmpty(title)) { tv_title.setVisibility(View.GONE); // view.setVisibility(View.GONE); } else { // 間隔線 // view.setVisibility(View.VISIBLE); tv_title.setVisibility(View.VISIBLE); tv_title.setText(title); } // 將自定義的布局設置上去 dialog.setContentView(layout); // 設置確定按鈕的文字以及點擊事件 Button confirm_btn = ((Button) layout .findViewById(R.id.confirm_btn)); if (TextUtils.isEmpty(positiveButtonText)) { confirm_btn.setText("確定"); } else { confirm_btn.setText(positiveButtonText); } confirm_btn.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { onDedindedClickedListener.onPositiveButtonClieked(dialog); dialog.dismiss(); } }); // 設置取消按鈕 Button cancle_btn = ((Button) layout.findViewById(R.id.cancle_btn)); if (TextUtils.isEmpty(negativeButtonText)) { cancle_btn.setText("取消"); } else { cancle_btn.setText(negativeButtonText); } cancle_btn.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { onDedindedClickedListener.onNegativeButtonClieked(dialog); dialog.dismiss(); } }); // 設置dialog的寬度 WindowManager.LayoutParams params = dialog.getWindow() .getAttributes(); params.width = getScreenWidth(context) - dp2px(context, 50); dialog.getWindow().setAttributes(params); return dialog; } /** * 獲得屏幕寬度 * * @param context * @return */ public int getScreenWidth(Context context) { WindowManager wm = (WindowManager) context .getSystemService(Context.WINDOW_SERVICE); DisplayMetrics outMetrics = new DisplayMetrics(); wm.getDefaultDisplay().getMetrics(outMetrics); return outMetrics.widthPixels; } /** * dp轉px * * @param context * @param val * @return */ public int dp2px(Context context, float dpVal) { return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpVal, context.getResources().getDisplayMetrics()); } } /** * 當點擊確定的時候會調用onPositiveButtonClieked 當點擊取消的時候會調用onPositiveButtonClieked * * @author Haipeng * * 2016-7-28 */ public interface OnDedindedClickedListener { void onPositiveButtonClieked(DefindedDialog dialog); void onNegativeButtonClieked(DefindedDialog dialog); } }

這裡有幾個關鍵設計要留意:

  • 這個類別繼承自 Dialog,而所有 Dialog 的設定都集中在內部的 Builder 類別裡處理。
  • 真正的 Dialog 實例是在 Builder 的 create() 方法中建立的。
  • Builder 的建構子會把所需參數一次接收進來,包含標題、內容、按鈕文字,以及兩個按鈕的點擊監聽。
  • OnDedindedClickedListener 介面的兩個方法,分別對應確定與取消按鈕的點擊行為。
  • 由於版面是在 create() 裡透過 setContentView 設定的,Dialog 的大小完全能由我們掌控,靈活性大幅提高。舉例來說,若日後需要三個按鈕的版本,只要在版面檔裡多加一個按鈕,程式碼稍作調整即可。

二、Dialog 的版面配置檔

          

三、定義 Dialog 的樣式

Builder 的 create() 方法用到了一个自訂樣式,用來去除系統預設的 Dialog 背景並隱藏標題列:

 

重點說明:

  • 這個樣式移除了 Dialog 的預設背景,並關閉標題顯示。
  • windowIsFloating 若設為 false,Window 會預設填滿整個螢幕;設為 true 時,Window 大小則改為 wrap_content,也就是依內容自動調整。

四、封裝成工具類

為了讓呼叫端更省事,我們把常用情境都封裝成靜態方法:

 /** * 創建Dialog的工具類 * * @author Haipeng * * 2016-7-28 */ public class DialogUtils { /** * 只要內容的dialog 兩個按鈕的文字設置為默認 不要標題 * * @param context * @param content * @param listener * @return */ public static Dialog commonDialogTwoBtn(Context context, String content, DefindedDialog.OnDedindedClickedListener listener) { return commonDialogTwoBtn(context, null, content, "確定", "取消", listener); } /** * 只要內容的dialog 兩個按鈕的文字設置為不默認 * * @param context * @param content * @param positiveName * @param negativeName * @param listener * @return */ public static Dialog commonDialogTwoBtn(Context context, String content, String positiveName, String negativeName, DefindedDialog.OnDedindedClickedListener listener) { return commonDialogTwoBtn(context, null, content, positiveName, negativeName, listener); } /** * 既要內容 又要標題的dialog 並且兩個按鈕的文字設置為默認 * * @param context * @param title * @param content * @param listener * @return */ public static Dialog commonDialogTwoBtn(Context context, String title, String content, DefindedDialog.OnDedindedClickedListener listener) { return commonDialogTwoBtn(context, title, content, "確定", "取消", listener); } /** * 既要內容 又要標題的dialog 並且兩個按鈕的文字不默認 * * @param context * @param title * @param content * @param positiveName * @param negativeName * @param listener * @return */ public static Dialog commonDialogTwoBtn(Context context, String title, String content, String positiveName, String negativeName, DefindedDialog.OnDedindedClickedListener listener) { if (TextUtils.isEmpty(content)) { return null; } if (listener == null) { return null; } DefindedDialog.Builder alert = new DefindedDialog.Builder(context, title, content, positiveName, negativeName, listener); DefindedDialog dialog = alert.create(); dialog.show(); return dialog; } }

這裡兩個按鈕的文案預設是「確定」與「取消」,當然你也可以傳入自訂文字,而標題則是可選的。

五、在 MainActivity 中呼叫測試

 private Button warming; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); warming = (Button) findViewById(R.id.warming); warming.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.warming: OnDedindedClickedListener onDedindedClickedListener = new DefindedDialog.OnDedindedClickedListener() { @Override public void onPositiveButtonClieked(DefindedDialog dialog) { Toast.makeText(MainActivity.this, "確定", Toast.LENGTH_SHORT) .show(); } @Override public void onNegativeButtonClieked(DefindedDialog dialog) { Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT) .show(); } }; DialogUtils.commonDialogTwoBtn(this, "你確定要刪除該商品嗎?", onDedindedClickedListener); DialogUtils.commonDialogTwoBtn(this, "刪除", "你確定要刪除該商品嗎?", onDedindedClickedListener); DialogUtils.commonDialogTwoBtn(this, "你確定要刪除該商品嗎?", "確定", "取消", onDedindedClickedListener); DialogUtils.commonDialogTwoBtn(this, "刪除", "你確定要刪除該商品嗎?", "確定", "取消", onDedindedClickedListener); break; } }

版面檔裡只放了一顆按鈕,為了方便一次比較四種呼叫方式,這裡在按下按鈕後一次彈出了四個 Dialog。透過這樣的封裝,日後要在專案裡大量重複使用彈窗時,只要一行程式碼就能搞定,維護起來輕鬆許多。

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