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>
时间: 2024-08-27 02:04:28

Android 自定义AlertDialog(退出提示框)的相关文章

Android使用自定义AlertDialog(退出提示框)

http://www.cnblogs.com/511mr/archive/2011/10/21/2220253.html

Android 退出提示框 代码

转自:http://hi.baidu.com/ittdt/item/d932cf37f486f886c3cf29ea new AlertDialog.Builder(MainEngine.context)    //.setTitle("提示")    .setMessage("确定要退出游戏吗?")    .setPositiveButton("确定", new DialogInterface.OnClickListener() {     @

Android 底部弹出提示框的解决办法(使用Activity以及PopupWindow)

本片文章主要谈探讨了如何实现在底部弹出提示框背景为半透明效果的实现.想要实现此种效果一般有两种方式一个是使用Activity设置Theme另一种方式就是使用PopupWindow设置样式实现效果. 一,使用Activity 首先是此activity的布局文件: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.andro

[原创] Android 自定义AlertDialog 去黑边终极解决方案(亲测有效!)

问题:自定义AlertDialog出现黑边 运行代码段: View view = View.inflate(context, R.layout.dialog_common, null); mDialog = new AlertDialog.Builder(context).create(); mDialog.setView(view); mDialog.show(); dialog_common.xml <?xml version="1.0" encoding="utf

Android 自定义AlertDialog 去黑边终极解决方案(亲测有效!)

问题:自定义AlertDialog出现黑边 运行代码段: View view = View.inflate(context, R.layout.dialog_common, null); mDialog = new AlertDialog.Builder(context).create(); mDialog.setView(view); mDialog.show(); dialog_common.xml <?xml version="1.0" encoding="utf

之前项目中用到的简单的自定义弹出提示框的实现,整理整理,当然开源的插件很多,但自己写的可以随意发挥

效果如下: html代码: <div class="container"> <div class="wrapper" style="background-color:white; position:relative;"> <div class="box" style="background-color:red; position:absolute; left:100px; top:300

PtQt5 closeEvent 关闭事件,退出提示框

如果关闭QWidget,就会产生一个QCloseEvent,并且把它传入到closeEvent函数的event参数中.改变控件的默认行为,就是替换掉默认的事件处理. 原生的英文提示框: # 添加一个退出的提示事件 def closeEvent(self, event): """我们创建了一个消息框,上面有俩按钮:Yes和No.第一个字符串显示在消息框的标题栏,第二个字符串显示在对话框, 第三个参数是消息框的俩按钮,最后一个参数是默认按钮,这个按钮是默认选中的.返回值在变量re

android基础篇:提示框AlertDialog实例

string.xml <?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">TestBase</string> <string name="city_name">城市</string> <string name="city_default&quo

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

JAVA: public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } //点击物理按钮时,执行退出提示 public boolean onKeyDown(int keyCode,KeyE