有时候我们需要在游戏或应用中用一些符合我们样式的提示框(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,例如支付页面,先弹出自定义AlertDialog询问是否愿意支付,确定则弹出另外一个自定义AlertDialog,选择支付方式,android自定义AlertDialog,android自定义AlertDialog,android自定义...
本文通过实例代码给大家详解Android使用自定义alertdialog实现确认退出按钮,对alertdialog退出按钮相关知识感兴趣的朋友一起学习吧
自定义AlertDialog对话框用于加载等耗时任务提示,博文地址http://blog.csdn.net/u012806692,在这个地址的内容中找下,因为刚写完还是404。。。。
主要介绍了Android编程之自定义AlertDialog(退出提示框)用法,结合实例形式较为详细的分析了自定义AlertDialog的页面布局与功能实现相关技巧,需要的朋友可以参考下
新手在自定义AlertDialog上的疑问笔者猜测主要有两个: 1、自定义的layout如何放到AlertDialog中? 解答: 获取到layout的view之后,直接调用AlertDialog.Builder的setView方法即可。 2、如何对自定义AlertDialog中...
最近项目里有个功能点,需要使用定制seekbar调节媒体音量,不能使用【按物理音量上下键】弹出的系统dialog做音量调节。我需要继承AlertDialog,设置自己的内容view,重写onKeyDown方法,设置dialog的位置等...... 写...
主要介绍了Android编程自定义AlertDialog样式的方法,结合实例形式详细分析了Android自定义AlertDialog样式的具体布局与功能实现相关操作技巧,需要的朋友可以参考下
这个是看了网上很多例子后,自己优化...用法和安卓原生的显示,可以由用户自行组合出自己想要的弹出框,不会高度定制限定死各种弹出框的UI,每个部件都是独立的,如标题,内容,左按钮,右按钮,底部一个按钮,list等。
android 自定义AlertDialog弹出键盘可以输入的
一个简单的自定义AlertDialog
Android自定义AlertDialog和ProgressDialog。
本篇文章主要介绍了Android仿IOS自定义AlertDialog对话框,主要介绍了圆角AlertDialog对话框,具有一定的参考价值,有兴趣的可以了解一下。
自定义AlertDialog(仿微信)
绝对给力,你想搞成什么颜色都可以的哦,在xml里面设置,好high的对话框,完全自定义Android对话框AlertDialog的实现,编译通过,代码完整
比较漂亮的自定义AlertDialog。
Android 自定义 AlertDialog