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

2023-12-12

我想显示一个对话框/弹出窗口,并向用户显示一条消息,显示“您确定要删除此条目吗?”带有一个“删除”按钮。什么时候Delete被触及,它应该删除该条目,否则什么也没有。

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


你可以使用AlertDialog为此并使用其构造一个Builder班级。下面的示例使用默认构造函数,该构造函数仅接受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();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 Android 上显示警报对话框? 的相关文章

随机推荐