发布于 

封装AlertDialog自定义布局

更轻松解决AlertDialog臃肿自定义实现

思路

封装其view方法 通过自定义接口提供给接口使用者进行其相关逻辑的操作 果然接口几乎是万金油的存在

实现
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
fun showAlertDialog(id: Int, confirmCallback: ConfirmCallback): View {
var show: AlertDialog? = null
val customizeDialog: AlertDialog.Builder = AlertDialog.Builder(context)
customizeDialog.setCancelable(false)
val infoview: View = LayoutInflater.from(context).inflate(
id,
null, false
)
customizeDialog.setView(infoview)
customizeDialog.setPositiveButton(
"确定"
) { dialog, which ->
confirmCallback.doWork()
show!!.dismiss()
}
show = customizeDialog.show()
return infoview
}
普通使用
1
2
3
4
5
6
7
8
9
10
11
var id: EditText? = null
val showCustomizeDialog = showAlertDialog(R.layout.iv_activity_user_shared_interface_activity_add_fragment,object : ConfirmCallback {
override fun doWork() {
if (id!!.text.isEmpty()) {
Toast.makeText(this@MainActivity, "内容为空",Toast.LENGTH_SHORT).show()
return
}
Toast.makeText(this@MainActivity, "测试成功", Toast.LENGTH_SHORT).show()
}
})
id = showCustomizeDialog.findViewById<EditText>(R.id.edit_iv_activity_user_shared_interface_activity_add_fragment_id)
注释

具体可访问Github