Android 用建造者模式模式写一个Dialog
建造者模式理解
建造者模式(Builder Pattern)使用多个简单的对象一步一步构建成一个复杂的对象。
这种类型的设计 模式属于创建型模式,它提供了一种创建对象的最佳方式。
何时使用:一些基本部件不会变,而其组合经常变化的时候。
如何解决:将变与不变分离开。
优点: 1、建造者独立,易扩展。 2、便于控制细节风险。
缺点: 1、产品必须有共同点,范围有限制。 2、如内部变化复杂,会有很多的建造类。
class CustomDialog(context: Context): Dialog(context,R.style.MyDialogStyle){ var title: String? = null//对话框标题 var message : String? = null//对话框内容 var confirmBtnText : String? = null //按钮名称“确定” var cancelBtnText: String? = null //按钮名称“取消” var mContentView: View? = null //对话框中间加载的其他布局界面 var isOutside: Boolean = true //对话框中间加载的其他布局界面 //按钮监听事件 var confirmBtnClickListener: DialogInterface.OnClickListener? = null var cancelBtnClickListener: DialogInterface.OnClickListener? = null abstract class Custom(context: Context) { var custom = CustomDialog(context) protected set abstract fun setTitle(title: String):Custom abstract fun setMessage(message: String):Custom abstract fun setContentView(contentView: View):Custom abstract fun isOutside(boolean: Boolean):Custom abstract fun setPositiveButton(confirm: String, listener: DialogInterface.OnClickListener):Custom abstract fun setNegativeButton(cancel: String, listener: DialogInterface.OnClickListener):Custom open fun build(): CustomDialog { val inflater = custom.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater val contentView: View = inflater.inflate(R.layout.dialog_custom_1, null) val titleView = contentView.findViewById<TextView>(R.id.title) val confirmBtn = contentView.findViewById<TextView>(R.id.confirm_btn) val cancelBtn = contentView.findViewById<TextView>(R.id.cancel_btn) val mTVMsg = contentView.findViewById<TextView>(R.id.message) val btnView = contentView.findViewById<View>(R.id.btn_view) val llMessage = contentView.findViewById<LinearLayout>(R.id.ll_message) val topline = contentView.findViewById<View>(R.id.ll_btn_topline) val llBtn = contentView.findViewById<LinearLayout>(R.id.ll_btn) custom.apply { setContentView(contentView) // 设置标题 title?.let { titleView.text = it titleView.paint.isFakeBoldText = true }?:run { titleView.visibility = View.GONE } // 设置确定按钮 confirmBtnText?.let { confirmBtn.text = it confirmBtn.setOnClickListener { confirmBtnClickListener?.onClick(this, Dialog.BUTTON_POSITIVE) dismiss() } }?:run { confirmBtn.visibility = View.GONE btnView.visibility = View.GONE } // 设置取消按钮 cancelBtnText?.let { cancelBtn.text = it cancelBtn.setOnClickListener { cancelBtnClickListener?.onClick(this, Dialog.BUTTON_POSITIVE) dismiss() } }?:run { cancelBtn.visibility = View.GONE btnView.visibility = View.GONE } if(confirmBtnText==null&&cancelBtnText==null){ llBtn.visibility = View.GONE topline.visibility = View.GONE }else{ llBtn.visibility = View.VISIBLE topline.visibility = View.VISIBLE } // 设置内容 message?.let { mTVMsg.text = it }?:run { mContentView.let { llMessage.removeView(mTVMsg) llMessage.addView( it, LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT ) ) } } //设置对话框大小 val dialogWindow: Window? = window val lp = dialogWindow?.attributes val d = context.resources.displayMetrics lp?.width = (d.widthPixels * 0.8).toInt() dialogWindow?.attributes = lp setCanceledOnTouchOutside(isOutside) } return custom } } class Builder(context: Context) : CustomDialog.Custom(context) { override fun setTitle(title: String) = with(this){ custom.title = title this } override fun setMessage(message: String) = with(this){ custom.message = message this } override fun setContentView(contentView: View) = with(this){ custom.mContentView = contentView this } override fun isOutside(boolean: Boolean) = with(this){ custom.isOutside = boolean this } override fun setPositiveButton(confirm: String, listener: DialogInterface.OnClickListener) = with(this){ custom.confirmBtnText = confirm custom.confirmBtnClickListener = listener this } override fun setNegativeButton(cancel: String, listener: DialogInterface.OnClickListener) = with(this){ custom.cancelBtnText = cancel custom.cancelBtnClickListener = listener this } } }
<style name="MyDialogStyle"> <item name="android:windowBackground">@android:color/transparent</item> <item name="android:windowFrame">@null</item> <item name="android:windowNoTitle">true</item> <item name="android:windowIsFloating">true</item> <item name="android:windowIsTranslucent">true</item> <item name="android:windowContentOverlay">@null</item> <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item> <item name="android:backgroundDimEnabled">true</item> </style>
<?xml version="1.0" encoding="UTF-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/dialog_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/login_bg" android:gravity="center_horizontal" android:orientation="vertical" android:layout_marginBottom="10dp"> <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="1dp" android:gravity="center_horizontal" android:padding="20dp" tools:text="@string/gps_tip" android:textColor="#282828" android:textSize="18sp" /> <LinearLayout android:id="@+id/ll_message" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:id="@+id/message" android:layout_width="wrap_content" android:layout_height="wrap_content" tools:text="确定执行该操作dsfdsfsdfsdfsdfsdfsdfsdfsdfsdfdsf?" android:textSize="16sp" android:gravity="center_horizontal" android:textColor="#383838" android:layout_marginBottom="20dp" /> </LinearLayout> <View android:id="@+id/ll_btn_topline" android:layout_width="match_parent" android:layout_height="1dp" android:background="#A8A7A7" /> <LinearLayout android:id="@+id/ll_btn" android:layout_width="match_parent" android:layout_height="50dp"> <TextView android:layout_marginTop="10dp" android:layout_marginBottom="10dp" android:id="@+id/cancel_btn" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:text="@string/cancel" android:textSize="16sp" android:focusable="true" android:clickable="true" android:gravity="center" android:textColor="#00B2EE" /> <View android:id="@+id/btn_view" android:layout_width="1dp" android:layout_height="match_parent" android:background="#A8A7A7" /> <TextView android:layout_marginTop="10dp" android:layout_marginBottom="10dp" android:id="@+id/confirm_btn" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:text="@string/sure" android:textSize="16sp" android:focusable="true" android:clickable="true" android:gravity="center" android:textColor="#E91E63" /> </LinearLayout> </LinearLayout>
CustomDialog customDialog= new CustomDialog.Builder(this) .setTitle("Title") .setMessage("Message") .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }) .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }) .build(); customDialog.show();