解散后重新显示对话框

时间:2017-08-27 23:29:17

标签: java android dialog kotlin listener

每次解雇后如何重新显示相同的对话框? 我的代码(在Kotlin中):

val alertDialog = AlertDialog.Builder(this)
alertDialog.setMessage(getString(R.string.alert_dialog_main_activity))
val editText = EditText(this)
editText.inputType = InputType.TYPE_CLASS_PHONE
alertDialog.run {
    setView(editText)
    setOnDismissListener { TODO( "Re-show dialog" }
    setPositiveButton("Done", { dialogInterface, i ->
        run {
            toast(editText.text.toString())
            PreferenceManager.getDefaultSharedPreferences(applicationContext).edit().putInt("phoneNumber", editText.text.toString().toInt()).commit()
            }
        })
    show()
}

答案也可能是Java。

2 个答案:

答案 0 :(得分:1)

最简单的方法可能是回忆显示对话框的功能,如下所示:

    fun showDialog() {
        val alertDialog = AlertDialog.Builder(this)
        alertDialog.setMessage(getString(R.string.alert_dialog_main_activity))
        val editText = EditText(this)
        editText.inputType = InputType.TYPE_CLASS_PHONE
        alertDialog.run {
            setView(editText)
            setOnDismissListener {
                 showDialog() // recall the function
            }
            setPositiveButton("Done", { dialogInterface, i -> {
                    toast(editText.text.toString())
                    PreferenceManager.getDefaultSharedPreferences(applicationContext).edit().putInt("phoneNumber", editText.text.toString().toInt()).commit()
                    }
                })
            show()
        }
    }

答案 1 :(得分:0)

试试这个:

AlertDialog.Builder(this).apply {
    setMessage(getString(R.string.alert_dialog_main_activity))
    setView(editText)
    setOnDismissListener {
        if(/*TODO validate phone number*/)
        (it as AlertDialog).show()
    }
    setPositiveButton("Done", { dialogInterface, i ->
        toast(editText.text.toString())
        PreferenceManager.getDefaultSharedPreferences(applicationContext).edit().putInt("phoneNumber", editText.text.toString().toInt()).commit()

    })
    show()
}