kotlin自定义对话框的具体实现代码
创建我们自己的对话框,继承于系统的Dialog 实现构造方法
class CommonDialog(context: Context?, themeResId: Int) : Dialog(context, themeResId) {}
2. 在内部创建BUilder类 定义出我们需要的方法和属性
class Builder (private val context: Context) { private var title: String? = null private var message: String? = null private var positiveButtonContent: String? = null private var negativeButtonContent: String? = null private var positiveButtonListener: ? = null private var negativeButtonListener: ? = null private var contentView: View? = null private var imageid: Int = 0 private var color: Int = 0 private var withOffSize: Float = () private var heightOffSize: Float = () fun setTitle(title: String): Builder { this.title = title return this } fun setTitle(title: Int): Builder { this.title = context.getText(title) as String return this } fun setMessage(message: String): Builder { this.message = message return this } fun setMessageColor(color: Int): Builder { this.color = color return this } fun setImageHeader(Imageid: Int): Builder { this.imageid = Imageid return this } fun setPositiveButton(text: String, listener: ): Builder { this.positiveButtonContent = text this.positiveButtonListener = listener return this } fun setPositiveButton(textId: Int, listener: ): Builder { this.positiveButtonContent = context.getText(textId) as String this.positiveButtonListener = listener return this } fun setNegativeButton(text: String, listener: ): Builder { this.negativeButtonContent = text this.negativeButtonListener = listener return this } fun setNegativeButton(textId: Int, listener: ): Builder { this.negativeButtonContent = context.getText(textId) as String this.negativeButtonListener = listener return this } fun setContentView(v: View): Builder { this.contentView = v return this } fun setWith(v: Float): Builder { this.withOffSize = v return this } fun setContentView(v: Float): Builder { this.heightOffSize = v return this } fun create(): CommonDialog { /** * 利用我们刚才自定义的样式初始化Dialog */ val dialog = CommonDialog(context, ) /** * 下面就初始化Dialog的布局页面 */ val inflater = context .getSystemService() as LayoutInflater val dialogLayoutView = (R.layout.dialog_layout, null) (dialogLayoutView, ( .WRAP_CONTENT, .WRAP_CONTENT)) if (imageid != 0) { (() as ImageView) .setImageResource(imageid) } else { (() as ImageView).visibility = } if (!(title)) { (() as TextView).text = title } else { // (().toString(), "未设置对话框标题!"); } if (color != 0) { val viewById = () as TextView viewById.setTextColor(color) } if (!(message)) { (() as TextView).text = message } else if (contentView != null) { (dialogLayoutView .findViewById<View>() as LinearLayout) .removeAllViews() (dialogLayoutView .findViewById<View>() as LinearLayout).addView( contentView, ( .WRAP_CONTENT, .WRAP_CONTENT)) } else { (() as TextView).visibility = } if (!(positiveButtonContent)) { (() as TextView).text = positiveButtonContent if (positiveButtonListener != null) { (() as TextView) .setOnClickListener { positiveButtonListener!!.onClick(dialog, -1) } } } else { (() as TextView).visibility = ().visibility = } if (!(negativeButtonContent)) { (() as TextView).text = negativeButtonContent if (negativeButtonListener != null) { (dialogLayoutView .findViewById<View>() as TextView) .setOnClickListener { negativeButtonListener!!.onClick(dialog, -2) } } } else { (() as TextView).visibility = } /** * 将初始化完整的布局添加到dialog中 */ (dialogLayoutView) /** * 禁止点击Dialog以外的区域时Dialog消失 */ (false) val window = dialog.window val context = this.context as Activity val windowManager = context.windowManager val defaultDisplay = windowManager.defaultDisplay val attributes = window!!.attributes if (() != ) { attributes.width = ( * withOffSize).toInt() } else { attributes.width = ( * ).toInt() } if (() != ) { attributes.height = ( * heightOffSize).toInt() } window.attributes = attributes return dialog } }
3.在需要的地方使用
(this). setImageHeader() .setTitle("你是否要注销账户") .setMessage("注销后需重新注册才能使用牛返返优惠") .setPositiveButton("确定注销", { p0, p1 -> p0?.dismiss() DestroyAccount() }) .setNegativeButton("取消", { p0, p1 -> p0?.dismiss() }) .setWith(f) .create() .show()
实现效果:
THE END