在onClickListener中关闭Anko对话框

时间:2018-10-22 13:13:23

标签: android kotlin anko

当我按customLayout中定义的按钮时,我想关闭Anko对话框

val dialog = alert {
   val view = layoutInflater.inflate(R.layout.match_stats, null)
   val closeButton = view.findViewById<ImageButton>(R.id.closeButton)
   closeButton.setOnClickListener { _ -> dialog.dismiss()}
   customView = view
}
dialog.show()

不幸的是,我尝试了上面的代码,但是在我的dialog中找不到对onClickListener的引用。你有解决的办法吗?

1 个答案:

答案 0 :(得分:2)

您可以在之前声明变量并分配HEAD

null

当然,现在您的var dialog: DialogInterface? = null dialog = alert { val view = layoutInflater.inflate(R.layout.match_stats, null) val closeButton = view.findViewById<ImageButton>(R.id.closeButton) closeButton.setOnClickListener { _ -> dialog?.dismiss()} customView = view }.show() 变量是可变的并且是可选的。

相关问题