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

编程入门 行业动态 更新时间:2024-10-11 09:25:26
本文介绍了如何在Android上显示警报对话框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想显示一个对话框/弹出窗口,并向用户显示您确定要删除此条目吗?"的消息.一个带有删除"按钮.触摸Delete时,它应删除该条目,否则将不删除任何内容.

I want to display a dialog/popup window with a message to the user that shows "Are you sure you want to delete this entry?" with one button that says 'Delete'. When Delete is touched, it should delete that entry, otherwise nothing.

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

I have written a click listener for those buttons, but how do I invoke a dialog or popup and its functionality?

推荐答案

您可以为此使用AlertDialog并使用其Builder类构造一个.下面的示例使用默认构造函数,该构造函数仅接受Context,因为对话框将从您传入的Context中继承适当的主题,但是还有一个构造函数,如果您将其指定为第二个参数,则可以指定特定的主题资源希望这样做.

You could use an AlertDialog for this and construct one using its Builder class. The example below uses the default constructor that only takes in a Context since the dialog will inherit the proper theme from the Context you pass in, but there's also a constructor that allows you to specify a specific theme resource as the second parameter if you desire to do so.

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上显示警报对话框?

本文发布于:2023-11-27 16:09:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1638691.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:警报   对话框   如何在   Android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!