OStack程序员社区-中国程序员成长平台

标题: android - 如何在 Android 上显示警报对话框? [打印本页]

作者: 菜鸟教程小白    时间: 2022-8-1 01:25
标题: android - 如何在 Android 上显示警报对话框?

我想向用户显示一个对话框/弹出窗口,其中显示“您确定要删除此条目吗?”一个按钮,上面写着“删除”。当Delete被触及,它应该删除那个条目,否则什么都没有。

我已经为这些按钮编写了一个点击监听器,但是如何调用对话框或弹出窗口及其功能?



Best Answer-推荐答案


您可以使用 AlertDialog为此并使用其 Builder 构建一个类(class)。下面的例子使用默认构造函数,它只接受 Context因为对话框将从您传入的 Context 继承正确的主题,但是如果您愿意,还有一个构造函数允许您指定特定的主题资源作为第二个参数。

new AlertDialog.Builder(context)
    .setTitle("Delete entry")
    .setMessage("Are you sure you want to delete this entry?")

    // Specifying a listener allows you to take an action before dismissing the dialog.
    // The dialog is automatically dismissed when a dialog button is clicked.
    .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) { 
            // Continue with delete operation
        }
     })

    // A null listener allows the button to dismiss the dialog and take no further action.
    .setNegativeButton(android.R.string.no, null)
    .setIcon(android.R.drawable.ic_dialog_alert)
    .show();

关于android - 如何在 Android 上显示警报对话框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2115758/






欢迎光临 OStack程序员社区-中国程序员成长平台 (http://ostack.cn/) Powered by Discuz! X3.4