Android实现“退出确认”对话框
@Override
public void onBackPressed() {
new AlertDialog.Builder(this).setTitle("确认退出吗?")
.setIcon(android.R.drawable.ic_dialog_info)
.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface dialog, int which) {
// 点击“确认”后的操作
MainFragmentActivity.this.finish(); }
})
.setNegativeButton("返回", new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface dialog, int which) {
// 点击“返回”后的操作,这里不设置没有任何操作
}
}).show();
// super.onBackPressed();
}
下面是用到的背景图或按钮的图片
经过查找资料和参考了一下例子后才知道,要实现这种效果很简单.就是在设置alertDialog的contentView.
以下的代码是写在Activity下的,代码如下:
public boolean onKeyDown(int keyCode, KeyEvent event) {
// 如果是返回键,直接返回到桌面
if(keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_HOME){
showExitGameAlert();
}
return super.onKeyDown(keyCode, event);
}
private void showExitGameAlert() {
final AlertDialog dlg = new AlertDialog.Builder(this).create();
dlg.show();
Window window = dlg.getWindow();
// *** 主要就是在这里实现这种效果的.
// 设置窗口的内容页面,shrew_exit_dialog.xml文件中定义view内容
window.setContentView(R.layout.shrew_exit_dialog);
// 为确认按钮添加事件,执行退出应用操作
ImageButton ok = (ImageButton) window.findViewById(R.id.btn_ok);
ok.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
exitApp(); // 退出应用...
}
});
// 关闭alert对话框架
ImageButton cancel = (ImageButton) window.findViewById(R.id.btn_cancel);
cancel.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
dlg.cancel();
}
});
}
以下的是layout文件,定义了对话框中的背景与按钮.点击事件在Activity中添加.
文件名为 : shrew_exit_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:Android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="wrap_content"> <!-- 退出游戏的背景图 -->
<ImageView android:id="@+id/exitGameBackground"
android:layout_centerInParent="true"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/bg_exit_game" /> <!-- 确认按钮 -->
<ImageButton android:layout_alignBottom="@+id/exitGameBackground"
android:layout_alignLeft="@+id/exitGameBackground"
android:layout_marginBottom="30dp"
android:layout_marginLeft="35dp"
android:id="@+id/btn_ok"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="@drawable/btn_ok" /> <!-- 取消按钮 -->
<ImageButton android:layout_alignBottom="@+id/exitGameBackground"
android:layout_alignRight="@+id/exitGameBackground"
android:layout_marginBottom="30dp"
android:layout_marginRight="35dp"
android:id="@+id/btn_cancel"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="@drawable/btn_cancel" />
</RelativeLayout>
就这样经过了以上几步,就可以实现自定义AlertDialog的效果了. 用同样的思路可以实现其它更复杂的效果.
Android实现“退出确认”对话框的更多相关文章
- Android:实现退出确认对话框
在Android平台上捕获Back键的事件,super.onBackPressed()是执行系统的默认动作,就是退出当前activity,我们要做的就是重写onBackPressed()函数, pub ...
- android 程序退出的对话框
package com.example.yanlei.yl; import android.graphics.Color; import android.support.v7.app.AppCompa ...
- Android 手机卫士--确认密码对话框编写
本文接着实现“确认密码”功能,也即是用户以前设置过密码,现在只需要输入确认密码 本文地址:http://www.cnblogs.com/wuyudong/p/5940718.html,转载请注明出处. ...
- Android界面设计之对话框——定制Toast、AlertDialog
一.概述 在界面设计中需要根据用户操作显示提示信息.出错信息等,就要用到对话框.Android实现提示信息显示常用有两种方式 1.Toast 2.AlertDialog 二.Toast Android ...
- 实现对gridview删除行时弹出确认对话框的一种简单方法
在VS2008提供的GridView中我们可以直接添加一个CommandField删除列:<asp:CommandField ShowDeleteButton="True" ...
- [android] 手机卫士自定义对话框布局
手机防盗页面部分 点击手机防盗,进行判断,如果没有设置密码,显示一个设置密码的对话框,如果已经设置密码了,弹出输入密码对话框 密码保存在SharedPreferences中,数据取出进行判断 自定义一 ...
- 我的Android进阶之旅------>Android安全退出应用程序的几种方式
当应用不再使用时,通常需要关闭应用,可以使用以下几种方法关闭android应用: 第一种方法:首先获取当前进程的id,然后杀死该进程. 建议使用这种方式 android.os.Process.kil ...
- Java Swing 自定义Dialog确认对话框
Java Swing 自定义Dialog 需求:当点击JFrame窗口的关闭按钮时,弹框询问是否确定关闭窗口,如果是则关闭程序,否就让弹框消失什么也不做(使用Dialog). 分析:虽然Java提供了 ...
- ASP.NET点击按钮弹出确认对话框方法
开发asp.net网页应用程序的时候,有些页面的按钮需要增加一个确认对话框,比如: 实现这个功能比较简单,代码这样写: Button.Attributes["onclick"] = ...
随机推荐
- Scala, Groovy, Clojure, Jython, JRuby and Java ----我们的工作语言
在曾经的一封邮件中,我指出在众多改变中,最明显的一个就是:在java领地上的JVM上使用其它流行的语言的发展变得越来越快.一些老的和新的创建的基于JVM的语言---JRuby 和 Jython ,Ja ...
- sim800L调试问题
SIM800L默认上电开机,若此时没有把rst和pwk引脚提前设置好,SIM800l会使stm32进入硬件中断(这可能是因为方面电源的原因导致的),同时sim800L开机后需要一定的时间稳定下来,建议 ...
- 剑指offer代码 vs2013执行
方法: 代码文件夹名称为:CodingInterviewChinese2-master 1. 用vs2013加载解决方案 .sln文件 2. 一个解决方案下面有多个项目,通过右键解决方案->属性 ...
- Redis(十四):主从复制
当数据量变得庞大的时候,读写分离还是很有必要的.同时避免一个redis服务宕机,导致应用宕机的情况,我们启用sentinel(哨兵)服务,实现主从切换的功能. 主从复制 Redis 支持简单且易用的主 ...
- 微服务实战-使用API Gateway
当你决定将应用作为一组微服务时,需要决定应用客户端如何与微服务交互.在单体式程序中,通常只有一组冗余的或者负载均衡的服务提供点.在微服务架构中,每一个微服务暴露一组细粒度的服务提供点.在本篇文章中,我 ...
- CentOS7 使用tab建补全命令
Centos7在使用最小化安装的时候,没有安装自动补全的包,需要自己手动安装,安装下面过滤出来的包 yum -y install bash-completion 安装完毕后退出bash重新登陆生效!
- Linux下编译C++程序遇到错误:undefined reference to `*::*
“undefined reference to”的意思是,该函数未定义. 如果使用的是g++,有以下检查方案: 如果提示未定义的函数是某个库的函数.检查库是否已经安装,并在编译命令中采用-l和-L参数 ...
- [每天一个Linux小技巧] 查看时钟源精度
$ dmesg | grep clock [0.000000] OMAP clocksource: GPTIMER1 at 24000000 Hz [0.000000] sched_clock: 3 ...
- CheckBox和richTextBox
namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { Initialize ...
- I2C和SPI
I2C(Inter-Integrated Circuit)总线 两线式串行总线,用于连接微控制器及其外围设备.是微电子通信控制领域广泛采用的一种总线标准.它是同步通信的一种特殊形式,具有接口线少,控制 ...