有时候我们需要在游戏或应用中用一些符合我们样式的提示框(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(退出提示框)的更多相关文章

  1. Android 自定义AlertDialog退出对话框

    Android 自定义AlertDialog退出对话框 转 https://blog.csdn.net/wkh11/article/details/53081634在项目中很多时候会出现点击返回键出现 ...

  2. uwp - ContentDialog - 自定义仿iphone提示框,提示框美化

    原文:uwp - ContentDialog - 自定义仿iphone提示框,提示框美化 为了实现我想要的效果花费了我很长时间,唉,当初英语不好好学,翻官网翻了半天才找到,分享给刚入门的新手. 首先看 ...

  3. (转载)Android自定义ProgressDialog进度等待框

    Android自定义ProgressDialog进度等待框 作者:无缘公子 字体:[增加 减小] 类型:转载 时间:2016-01-11我要评论 这篇文章主要介绍了Android自定义Progress ...

  4. Android基础TOP4_1:点击物理按钮弹出退出提示框

    JAVA: public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedIns ...

  5. Android开发之AlertDialog警告提示框删除与取消 详解代码

    package cc.jiusansec.www; import android.app.Activity; import android.app.AlertDialog; import androi ...

  6. mono for android 第四课--提示框(转)

    其实在VS中开发安卓代码和C#还是有一些相似性,刚开始我也不知道怎么弹出提示框,于是就百度了下,再加上个人的小聪明得到一下结果 builder.setTitle表示提示框的标题. setMessage ...

  7. Android开发工程师文集-提示框,菜单,数据存储,组件篇

    提示框,菜单,数据存储,组件篇 Toast Toast.makeText(context, text, 时间).show(); setDuration();//设置时间 setGravity();// ...

  8. Android弹出输入提示框--PopupWindow实现

    前言  之前直接用Dialog实现了弹出对话框.现在尝试用更好地解决方案--PopupWindow类--来实现 1.首先搞一个弹出框布局,和之前类似. 这样的东西,它的布局是这样: 1 <?xm ...

  9. Android 退出提示框 代码

    转自:http://hi.baidu.com/ittdt/item/d932cf37f486f886c3cf29ea new AlertDialog.Builder(MainEngine.contex ...

随机推荐

  1. 在操作Centos系统时经常出现You have new mail in /var/spool/mail/root提示怎么回事?

    例如,在命令窗口中输入date查看时间,下面会出现一行提示 实际上,该功能为Linux操作系统核对系统资源状态并汇总,默认发送到root用户的/var/spool/mail/root目录,并在标准输出 ...

  2. PHP基础记录

    1. require和require_once的区别 require_once()包涵是绝对路径 include() 和require() :语句包括并运行指定文件. include() 产生一个警告 ...

  3. Spring Security构建Rest服务-0801-短信验证码发送

    实现短信验证码登录 开发短信验证码接口 校验短信验证码并登录 短信验证码和图片验证码开发思路类似: 1,我们访问一个controller 2,在controller里调用短信验证码生成接口生成验证码 ...

  4. Linux 系统计算文件夹下文件数量数目

    查看某目录下文件的个数(未包括子目录) ls -l |grep "^-"|wc -l 或 find ./company -type f | wc -l 查看某目录下文件的个数,包括 ...

  5. 利用System.IO.Compression操作压缩文件

    引用: using System.IO.Compression; using (FileStream zipToOpen = new FileStream(@"D:\json.zip&quo ...

  6. saltstack快速入门

    SALTSTACK是什么? Salt是一种和以往不同的基础设施管理方法,它是建立在大规模系统高速通讯能力可以大幅提升的想法上.这种方法使得Salt成为一个强大的能够解决基础设施中许多特定问题的多任务系 ...

  7. NoSQL之Redis入门笔记

    Redis 1.Redis介绍 1.1 NoSQL:一类新出现的数据库(not only sql),它的特点 不支持sql语法 存储结构跟传统关系型数据库中的那种关系表完全不同,nosql中存储的数据 ...

  8. 自然语言处理--N-gram

    考虑一个语音识别系统,假设用户说了这么一句话:“I have a gun”,因为发音的相似,该语音识别系统发现如下几句话都是可能的候选:1.I have a gun. 2.I have a gull. ...

  9. Linux网络子系统

    再Linux的世界里,万物皆文件,通过虚拟文件系统VFS,程序可以用标准的Linux系统调用对不同的文件系统,甚至不同介质上的文件系统进行读写操作.下面我们揭示Linux网络子系统的秘密 sockfs ...

  10. The "tsconfig.json" file must have compilerOptions.sourceMap set to true

    在编译ionic项目的时候出现:Error:The "tsconfig.json" file must have compilerOptions.sourceMap set to ...