我要创建的是一个自定义AlertDialogBox,我想针对其调用class AlertBox(context: Context) : BaseDialogHelper() {
override val dialogView: View by lazy {
LayoutInflater.from(context).inflate(R.layout.custom_dialog, null)
}
override val builder: AlertDialog.Builder = AlertDialog.Builder(context).setView(dialogView)
val txt_title: TextView by lazy {
dialogView.findViewById<TextView>(R.id.txt_title)
}
val txt_message: TextView by lazy {
dialogView.findViewById<TextView>(R.id.txt_message)
}
val btn_ok: Button by lazy {
dialogView.findViewById<Button>(R.id.btn_done)
}
val btn_cancel: Button by lazy {
dialogView.findViewById<Button>(R.id.btn_cancel)
}
//These are the methods that i want to change
fun setTitle(title: String) {
txt_title.text = title
}
fun setMessage(message: String) {
txt_message.text = message
}
fun onOkButtonClickListener(func: (() -> Unit)? = null) = with(btn_ok) {
setClickListenerToButton(func)
}
fun onCancelButtonClickListener(func: (() -> Unit)? = null) = with(btn_cancel) {
setClickListenerToButton(func)
}
private fun View.setClickListenerToButton(func: (() -> Unit)?) = setOnClickListener {
func?.invoke()
dialog?.dismiss()
}
}
的“ setTitle()”方法。我当前的AlertDialog代码如下所示
{{1}}
我想创建一个类似于TextView.setText(String)的类成员函数,该函数可以作为TextView.text =“”来调用。可以对自定义类执行此操作吗??
答案 0 :(得分:2)
您需要定义一个属性:
var text: String
get() = txt_title.text
set(value) { txt_title.text = value }