A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 夏落若 中级黑马   /  2013-6-20 00:43  /  1637 人查看  /  6 人回复  /   1 人收藏 转载请遵从CC协议 禁止商业使用本文

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

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


效果图:



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

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

以下的代码是写在Activity下的,代码如下:
  1. <font color="Black">public boolean onKeyDown(int keyCode, KeyEvent event) {
  2. // 如果是返回键,直接返回到桌面
  3. if(keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_HOME){
  4.            showExitGameAlert();
  5. }

  6. return super.onKeyDown(keyCode, event);
  7. }
  8. private void showExitGameAlert() {
  9. final AlertDialog dlg = new AlertDialog.Builder(this).create();
  10. dlg.show();
  11. Window window = dlg.getWindow();
  12.         // *** 主要就是在这里实现这种效果的.
  13.         // 设置窗口的内容页面,shrew_exit_dialog.xml文件中定义view内容
  14. window.setContentView(R.layout.shrew_exit_dialog);
  15.         // 为确认按钮添加事件,执行退出应用操作
  16. ImageButton ok = (ImageButton) window.findViewById(R.id.btn_ok);
  17. ok.setOnClickListener(new View.OnClickListener() {
  18.   public void onClick(View v) {
  19.    exitApp(); // 退出应用...
  20.   }
  21. });

  22.         // 关闭alert对话框架
  23.         ImageButton cancel = (ImageButton) window.findViewById(R.id.btn_cancel);
  24.         cancel.setOnClickListener(new View.OnClickListener() {
  25.    public void onClick(View v) {
  26.     dlg.cancel();
  27.   }
  28.    });
  29. }以下的是layout文件,定义了对话框中的背景与按钮.点击事件在Activity中添加.

  30. 文件名为 : shrew_exit_dialog.xml

  31. <?xml version="1.0" encoding="utf-8"?>
  32. <RelativeLayout
  33. xmlns:Android="http://schemas.android.com/apk/res/android"
  34. android:layout_height="wrap_content"
  35. android:layout_width="wrap_content">

  36. <!-- 退出游戏的背景图 -->
  37. <ImageView android:id="@+id/exitGameBackground"
  38.   android:layout_centerInParent="true"
  39.   android:layout_height="wrap_content"
  40.   android:layout_width="wrap_content"
  41.   android:src="@drawable/bg_exit_game" />

  42. <!-- 确认按钮 -->
  43. <ImageButton android:layout_alignBottom="@+id/exitGameBackground"
  44.   android:layout_alignLeft="@+id/exitGameBackground"
  45.   android:layout_marginBottom="30dp"
  46.   android:layout_marginLeft="35dp"
  47.   android:id="@+id/btn_ok"
  48.   android:layout_height="wrap_content"
  49.   android:layout_width="wrap_content"
  50.   android:background="@drawable/btn_ok" />

  51. <!-- 取消按钮 -->
  52. <ImageButton android:layout_alignBottom="@+id/exitGameBackground"
  53.   android:layout_alignRight="@+id/exitGameBackground"
  54.   android:layout_marginBottom="30dp"
  55.   android:layout_marginRight="35dp"
  56.   android:id="@+id/btn_cancel"
  57.   android:layout_height="wrap_content"
  58.   android:layout_width="wrap_content"
  59.   android:background="@drawable/btn_cancel" />
  60. </RelativeLayout></font>
复制代码
就这样经过了以上几步,就可以实现自定义AlertDialog的效果了. 用同样的思路可以实现其它更复杂的效果.

6 个回复

倒序浏览
好吧,我想快捷退出。
回复 使用道具 举报
如果赶上你的技术  我还需要时间
回复 使用道具 举报
这个实用好 还不要金币哈哈

点评

哈哈。。。。。。  发表于 2013-8-13 11:54
回复 使用道具 举报
跪求源码,加图片
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马