android警告框代码,如何在Android上显示警告对话框?

博客介绍了在Android中创建警告框的代码。使用AlertDialog的Builder类构造警告框,示例代码展示了设置标题、消息、确定和取消按钮,以及图标等操作,还说明了点击按钮后的处理逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

你可以用一个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();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值