在 onClickListener 中关闭 Anko 对话框
Dismissing Anko Dialog in onClickListener
我想在按下自定义布局中定义的按钮时关闭 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()
我尝试了上面的代码,不幸的是,我无法在我的 onClickListener
中获得对 dialog
的引用。您知道如何解决吗?
您可以在之前声明变量并赋值 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()
当然,现在您的 dialog
变量是可变的和可选的。
我想在按下自定义布局中定义的按钮时关闭 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()
我尝试了上面的代码,不幸的是,我无法在我的 onClickListener
中获得对 dialog
的引用。您知道如何解决吗?
您可以在之前声明变量并赋值 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()
当然,现在您的 dialog
变量是可变的和可选的。