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 ...
随机推荐
- 【bzoj4503】 两个串 FFT
$FFT$套路题(然而我看错题了) 我们考虑化一下式子. 设当前比较的两个部分为$S[i....i+|T|-1]$和$T[0....|T|-1]$. 我们对串$T$中出现问号的位置全部赋值为$0$. ...
- 【BZOJ2300】【HAOI2011】防线修建
题目大意:给你m+3个点,有q个操作,每次要么询问当前点集构所构成的上凸壳总长度,要么在当前点集中删除一个点. 这题是吼题啊!!! 刚开始想着如何正常地做,考虑过用线段树维护一个区间内的凸包,发现并不 ...
- 解决input和button错位(不齐)问题
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- centos7.2 get pid by process name with python3.6
centos7.2 get pid by process name with python3.6 #-*- encoding:UTF-8 -*- import os import sys import ...
- centos 7 keepalived故障邮件通知实战(附Python邮件发送脚本)
centos 7 keepalived故障邮件通知实战(附Python邮件发送脚本) ##################### sendmail.py begin ######## ...
- 【Java并发编程】:并发新特性—障碍器CyclicBarrier
CyclicBarrier(又叫障碍器)同样是Java5中加入的新特性,使用时需要导入Java.util.concurrent.CylicBarrier.它适用于这样一种情况:你希望创建一组任务,它们 ...
- certificate verify fails (https://gems.ruby-china.org错误
首先:执行这一步报错的背景是: 更换gems源, 通常执行 gem sources --add https://gems.ruby-china.org/ --remove https://rubyge ...
- PHP CURL 抓取失败 自己调试
蛋疼的一下午,物理机win7旗舰版+APACHE2 ,CURL抓取一直成功. 虚拟机ubuntu+apache2一直抓取失败. 晚上,问个仁兄才知道,CURL可以调试: 参考这位兄台: 地址 curl ...
- ZigBee 学习资源
1.雪帕的主页 http://home.cnblogs.com/u/yqh2007/ 2.刘志鹏的主页 http://www.cnblogs.com/hustlzp/archive/2011/02/1 ...
- C/C++ -- Gui编程 -- Qt库的使用 -- Qt5试用
1.头文件<QtGui>变成了<QtWidgets> 相应<QtGui/***>变成了<QtWidgets/***> 2.QTextCodec::set ...