我应该覆盖AlertDialog吗?

时间:2019-07-02 20:04:20

标签: android kotlin

我具有创建对话框的功能

fun abs(){
val builder = AlertDialog.Builder(context)
//and so on
}

我想使用setOnCancelListener

builder.setCanceledOnTouchOutside(true)
builder.setOnCancelListener(DialogInterface.OnCancelListener {
someFunction(view)
})

主要问题是在取消构建器/对话框之前必须使用someFunction(view)。 我该如何实现?我需要重写AlertDialog的某些功能吗?

1 个答案:

答案 0 :(得分:0)

尝试一下

private fun showAlertDialog(){
    val dialog = AlertDialog.Builder(this)
        .setTitle("title")
        .setMessage("message")
        .setCancelable(false)
        .setPositiveButton("OK", null)
        .setNegativeButton("Cancel", null)
        .create()

    dialog.setOnShowListener(object: DialogInterface.OnShowListener{
        override fun onShow(p0: DialogInterface?) {
            dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setOnClickListener{
                someFunction()
                dialog.dismiss()
            }
        }
    })
}
相关问题