简体   繁体   中英

Re-showing dialog after dissmiss

how to re-show the same dialog after every dismiss? My code(in 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()
}

Answers could be in Java too.

The easiest way would be probably to recall the function displaying the dialog, like this:

    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()
        }
    }

Try with this :

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()
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM