java - 可以询问您“是否要关闭它”的退出按钮?
问题描述
我是java的初学者,我有一个问题要问你你知道如何创建退出按钮吗?这个按钮可以在我关闭应用程序之前问我“你想关闭这个应用程序吗?或者”你确定要关闭它吗?“我需要为我的项目做这件事,我需要帮助。请给我一些代码。
解决方案
您的问题非常广泛,但是,您正在寻找一个 AlertDialog ,这是实现:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
MyActivity.this.finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
推荐阅读
- jquery - 多级菜单上的 Jquery 脚本,一次激活一个子菜单
- excel - 如何复制和粘贴过滤后的功率数据透视表
- java - 在一个简单的秒表上将计时器 TextView 格式化为 [m:ss]
- javascript - 如何生成不重复的随机结果?
- python - 为了将我的模型保存到我的文件路径,val_acc 没有确定要改进。我做错了什么
- css - Angular SlickGrid 是否可以使用分页并根据值设置行颜色?
- powershell - PowerShell脚本检查文件夹中的文件
- r - 计算增长率
- openssl - OpenSSL PKCS7_NOCHAIN 标志的验证过程是什么
- elasticsearch - 如何将 Kibana 与 nagios 集成?