我想用警告对话框替换 Snackbar,用 Kotlin 怎么做?

3 浏览
0 Comments

我想用警告对话框替换 Snackbar,用 Kotlin 怎么做?

这个问题已经有了答案

我如何在Android上显示警报对话框?

Snackbar.make(findViewById(android.R.id.content),\"Restart to update\", Snackbar.LENGTH_INDEFINITE)

我想把这个改成警报对话框,需要帮助。

admin 更改状态以发布 2023年5月24日
0
0 Comments

您可以在这里找到官方文档。

要实现一个AlertDialog,您需要使用AlertDialog.Builder类来设置AlertDialog。然后,您可以使用.create()方法来创建AlertDialog,最后使用AlertDialog类的.show()来显示对话框。

val alertDialog: AlertDialog? = activity?.let {
    val builder = AlertDialog.Builder(it)
    builder.apply {
        setMessage("This is the message")
        setTitle("This is the title")
        setPositiveButton(R.string.ok,
                DialogInterface.OnClickListener { dialog, id ->
                    // User clicked OK button
                })
        setNegativeButton(R.string.cancel,
                DialogInterface.OnClickListener { dialog, id ->
                    // User cancelled the dialog
                    // Use this to dismiss dialog
                    dialog.dismiss()
                })
    }
    // Set other dialog properties
    ...
    // Create the AlertDialog
    builder.create()
}
alertDialog.show()

您可以使用它来dismiss对话框:

alertDialog.dismiss()

0