我想用警告对话框替换 Snackbar,用 Kotlin 怎么做?
我想用警告对话框替换 Snackbar,用 Kotlin 怎么做?
这个问题已经有了答案:
Snackbar.make(findViewById(android.R.id.content),\"Restart to update\", Snackbar.LENGTH_INDEFINITE)
我想把这个改成警报对话框,需要帮助。
admin 更改状态以发布 2023年5月24日
您可以在这里找到官方文档。
要实现一个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()