有时候我们需要在游戏或应用中用一些符合我们样式的提示框(AlertDialog)

以下是我在开发一个小游戏中总结出来的.希望对大家有用.

先上效果图:

下面是用到的背景图或按钮的图片

经过查找资料和参考了一下例子后才知道,要实现这种效果很简单.就是在设置alertDialog的contentView.

以下的代码是写在Activity下的,代码如下:

  1. public boolean onKeyDown(int keyCode, KeyEvent event) {
  2. // 如果是返回键,直接返回到桌面
  3. if(keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_HOME){
  4. showExitGameAlert();
  5. }
  6.  
  7. return super.onKeyDown(keyCode, event);
  8. }
  9. private void showExitGameAlert() {
  10. final AlertDialog dlg = new AlertDialog.Builder(this).create();
  11. dlg.show();
  12. Window window = dlg.getWindow();
  13. // *** 主要就是在这里实现这种效果的.
  14. // 设置窗口的内容页面,shrew_exit_dialog.xml文件中定义view内容
  15. window.setContentView(R.layout.shrew_exit_dialog);
  16. // 为确认按钮添加事件,执行退出应用操作
  17. ImageButton ok = (ImageButton) window.findViewById(R.id.btn_ok);
  18. ok.setOnClickListener(new View.OnClickListener() {
  19. public void onClick(View v) {
  20. exitApp(); // 退出应用...
  21. }
  22. });
  23.  
  24. // 关闭alert对话框架
  25. ImageButton cancel = (ImageButton) window.findViewById(R.id.btn_cancel);
  26. cancel.setOnClickListener(new View.OnClickListener() {
  27. public void onClick(View v) {
  28. dlg.cancel();
  29. }
  30. });
  31. }

以下的是layout文件,定义了对话框中的背景与按钮.点击事件在Activity中添加.

文件名为 : shrew_exit_dialog.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout
  3.   xmlns:Android="http://schemas.android.com/apk/res/android"
  4.    android:layout_height="wrap_content"
  5.   android:layout_width="wrap_content">
  6.  
  7. <!-- 退出游戏的背景图 -->
  8. <ImageView android:id="@+id/exitGameBackground"
  9.    android:layout_centerInParent="true"
  10.   android:layout_height="wrap_content"
  11.   android:layout_width="wrap_content"
  12.    android:src="@drawable/bg_exit_game" />
  13.  
  14. <!-- 确认按钮 -->
  15. <ImageButton android:layout_alignBottom="@+id/exitGameBackground"
  16.    android:layout_alignLeft="@+id/exitGameBackground"
  17.    android:layout_marginBottom="30dp"
  18.    android:layout_marginLeft="35dp"
  19.    android:id="@+id/btn_ok"
  20.    android:layout_height="wrap_content"
  21.    android:layout_width="wrap_content"
  22.    android:background="@drawable/btn_ok" />
  23.  
  24. <!-- 取消按钮 -->
  25. <ImageButton android:layout_alignBottom="@+id/exitGameBackground"
  26.   android:layout_alignRight="@+id/exitGameBackground"
  27.    android:layout_marginBottom="30dp"
  28.    android:layout_marginRight="35dp"
  29.    android:id="@+id/btn_cancel"
  30.    android:layout_height="wrap_content"
  31.    android:layout_width="wrap_content"
  32.    android:background="@drawable/btn_cancel" />
  33. </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. 【NOIP2013】 火柴排队 贪心+splay

    这题为啥我写得这么复杂. 首先我们不难发现,我们将序列$a$和序列$b$排序,考虑两序列内无相同元素,那么最小值显然为$\sum_{i=1}^{n} (a_i-b_i)^2$. 下面考虑做法 首先,我 ...

  2. Python shutil模块(目录和文件操作)

    import shutil #导入shutil模块 copyfileobj方法 将类文件对象fsrc的内容复制到类文件对象fdst shutil.copyfileobj(fsrc, fdst[, le ...

  3. (转)Python标准库:内置函数print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)

    原文:https://blog.csdn.net/caimouse/article/details/44133241 https://www.cnblogs.com/owasp/p/5372476.h ...

  4. photoshop切图介绍 && photoshop下载与破解

    第一部分:界面设置 1.点击“文件-新建”(或者ctrl+n)打开一个新建对话框.名称可随意填写.“预设”设置为自定,“宽度”一般选择1920,“单位”选为像素.“高度”可选择为2000,“单位”选为 ...

  5. Cygwin安装配置

    1.下载安装Cygwin   我们可以到Cygwin的官方网站下载Cygwin的安装程序,地址是: http://www.cygwin.com/ 或者直接使用下载连接来下载安装程序,下载连接是: ht ...

  6. tensorflow进阶篇-3

    #-*- coding:utf-8 -*- #Tensorflow的嵌入Layer import numpy as np import tensorflow as tf sess=tf.Session ...

  7. Maven 学习笔记(二)

    项目最近开始使用maven去管理项目啦,说真的对于maven是一窍不通啊,今天和同事在回家的路上聊天的时候同事说他去第一家公司面试的时候人家问他 maven 怎么打包,当时我就懵逼了,因为我也不知道啊 ...

  8. 体验 QQ机器人C# SDK 1.X 特性总结

    主要特性 依赖注入 框架本身采用 Autofac 作为依赖注入框架.进行插件开发时,必然会使用到该框架.建议开发者阅读官方文档熟悉其用法.https://autofac.readthedocs.io/ ...

  9. [磁盘空间]lsof处理文件恢复、句柄以及空间释放问题

    曾经在生产上遇到过一个df 和 du出现的结果不一致的问题,为了排查到底是哪个进程占用了文件句柄,导致空间未释放,首先在linux上面,一切皆文件,这个问题可以使用lsof这个BT的命令来处理(这个哈 ...

  10. 常常忘记但是很重要的sql语句

    一.基础1.说明:创建数据库CREATE DATABASE database-name 2.说明:删除数据库drop database dbname3.说明:备份sql server--- 创建 备份 ...