kotlin使用建造者模式自定义对话框使用建造者模式自定义对话框
本文实例为大家分享了kotlin自定义对话框的具体代码,供大家参考,具体内容如下
1.CommonDialog 创建我们自己的对话框,继承于系统的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: DialogInterface.OnClickListener? = null
private var negativeButtonListener: DialogInterface.OnClickListener? = null
private var contentView: View? = null
private var imageid: Int = 0
private var color: Int = 0
private var withOffSize: Float = 0.toFloat()
private var heightOffSize: Float = 0.toFloat()
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: DialogInterface.OnClickListener): Builder {
this.positiveButtonContent = text
this.positiveButtonListener = listener
return this
}
fun setPositiveButton(textId: Int, listener: DialogInterface.OnClickListener): Builder {
this.positiveButtonContent = context.getText(textId) as String
this.positiveButtonListener = listener
return this
}
fun setNegativeButton(text: String, listener: DialogInterface.OnClickListener): Builder {
this.negativeButtonContent = text
this.negativeButtonListener = listener
return this
}
fun setNegativeButton(textId: Int, listener: DialogInterface.OnClickListener): Builder {
this.negativeButtonContent = context.getText(textId) as String
this.negativeButtonListener = listener
评论0
最新资源