Android 自定义AlertDialog(退出提示框)
有时候我们需要在游戏或应用中用一些符合我们样式的提示框(AlertDialog)
以下是我在开发一个小游戏中总结出来的.希望对大家有用.
先上效果图:

下面是用到的背景图或按钮的图片
经过查找资料和参考了一下例子后才知道,要实现这种效果很简单.就是在设置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>
Android 自定义AlertDialog(退出提示框)的更多相关文章
- Android 自定义AlertDialog退出对话框
Android 自定义AlertDialog退出对话框 转 https://blog.csdn.net/wkh11/article/details/53081634在项目中很多时候会出现点击返回键出现 ...
- uwp - ContentDialog - 自定义仿iphone提示框,提示框美化
原文:uwp - ContentDialog - 自定义仿iphone提示框,提示框美化 为了实现我想要的效果花费了我很长时间,唉,当初英语不好好学,翻官网翻了半天才找到,分享给刚入门的新手. 首先看 ...
- (转载)Android自定义ProgressDialog进度等待框
Android自定义ProgressDialog进度等待框 作者:无缘公子 字体:[增加 减小] 类型:转载 时间:2016-01-11我要评论 这篇文章主要介绍了Android自定义Progress ...
- Android基础TOP4_1:点击物理按钮弹出退出提示框
JAVA: public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedIns ...
- Android开发之AlertDialog警告提示框删除与取消 详解代码
package cc.jiusansec.www; import android.app.Activity; import android.app.AlertDialog; import androi ...
- mono for android 第四课--提示框(转)
其实在VS中开发安卓代码和C#还是有一些相似性,刚开始我也不知道怎么弹出提示框,于是就百度了下,再加上个人的小聪明得到一下结果 builder.setTitle表示提示框的标题. setMessage ...
- Android开发工程师文集-提示框,菜单,数据存储,组件篇
提示框,菜单,数据存储,组件篇 Toast Toast.makeText(context, text, 时间).show(); setDuration();//设置时间 setGravity();// ...
- Android弹出输入提示框--PopupWindow实现
前言 之前直接用Dialog实现了弹出对话框.现在尝试用更好地解决方案--PopupWindow类--来实现 1.首先搞一个弹出框布局,和之前类似. 这样的东西,它的布局是这样: 1 <?xm ...
- Android 退出提示框 代码
转自:http://hi.baidu.com/ittdt/item/d932cf37f486f886c3cf29ea new AlertDialog.Builder(MainEngine.contex ...
随机推荐
- UIDevice currentDevice model possible values
NOTE: The below code may not contain all device's string, I'm with other guys are maintaining the sa ...
- Oracle PL/SQL学习之基础篇(2)--例外
1.例外分类:系统例外.自定义例外 (1)系统例外,参见相关API文档 (2)自定义例外 定义自己的例外:就像自定义变量一样,类型为exception 抛出例外:使用raise抛出自定义例外 set ...
- 剑指offer二十九之最小的K个数
一.题目 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,. 二.思路 详解代码. 三.代码 import java.util. ...
- 修改WAMPServer(Apache+PHP+MySQL一键式安装)中mysql默认空密码
Note:在EclipsePHP中配置WorkSpace时,将工作目录指到执行PHP代码的www目录下 ,便于在Eclipse下编写PHP项目 eg:D:\KelvinSoftwar ...
- 使用openssh-clients的scp命令来传输文件
了解openssh-client是请参阅:https://blog.csdn.net/u010215256/article/details/53239905 了解scp命令来传输文件请参阅:https ...
- 目录打散-hash算法
前几篇说了文件上传,都是上传到了WebRoot下的up目录,这样是不行的,文件多了性能就不行了.文件一般都是分目录存放的,这里讲建目录的一种算法.先看结果,经过本算法建的目录,结构是这样的,还以up目 ...
- [转]C# 理解lock
原文:http://www.cnblogs.com/apsnet/archive/2012/07/08/2581475.html 一. 为什么要lock,lock了什么? 当我们使用线程的时候,效率最 ...
- gulp4.0 前端构建脚手架
最近看了下gulp4.0的升级,感觉和3.0相比变化还是比较大的,很多3.0的写法和插件会出现一些莫名其妙的变化,详细的变化就先不说了,这里我直接把我配置好的代码拿过来吧,方便各位可以更好的学习和使用 ...
- haml参考大全
原文来自: http://blackanger.blog.51cto.com/140924/47642 Haml是一种用来描述任何XHTML web document的标记语言,它是干净,简单的. ...
- SpringBoot自动化配置的注解开关原理
我们以一个最简单的例子来完成这个需求:定义一个注解EnableContentService,使用了这个注解的程序会自动注入ContentService这个bean. @Retention(Retent ...