Android使用自定义AlertDialog(退出提示框)--通过window来的

有时候我们需要在游戏或应用中用一些符合我们样式的提示框(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>就这样经过了以上几步,就可以实现自定义AlertDialog的效果了. 用同样的思路可以实现其它更复杂的效果.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您介绍一下Android仿IOS自定义AlertDialog提示框的实现方法。 首先,我们需要在Android项目中创建一个自定义布局文件,用于显示弹框的内容。可以使用LinearLayout或RelativeLayout等布局容器来组织弹框的内容,例如: ```xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/title" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="标题" android:textSize="18sp" /> <TextView android:id="@+id/message" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="内容" android:textSize="14sp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center"> <Button android:id="@+id/confirm" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="确定" /> <Button android:id="@+id/cancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="取消" /> </LinearLayout> </LinearLayout> ``` 接下来,我们需要创建一个自定义AlertDialog类,用于显示弹框和处理按钮点击事件。在这个类中,我们需要实现onCreateDialog方法来加载自定义布局文件,并设置弹框的标题、内容和按钮监听器等。例如: ```java public class IOSAlertDialog extends DialogFragment { private String title; private String message; private DialogInterface.OnClickListener confirmListener; private DialogInterface.OnClickListener cancelListener; public IOSAlertDialog(String title, String message, DialogInterface.OnClickListener confirmListener, DialogInterface.OnClickListener cancelListener) { this.title = title; this.message = message; this.confirmListener = confirmListener; this.cancelListener = cancelListener; } @Override public Dialog onCreateDialog(Bundle savedInstanceState) { LayoutInflater inflater = LayoutInflater.from(getActivity()); View view = inflater.inflate(R.layout.dialog_ios_alert, null); TextView titleView = view.findViewById(R.id.title); TextView messageView = view.findViewById(R.id.message); Button confirmButton = view.findViewById(R.id.confirm); Button cancelButton = view.findViewById(R.id.cancel); titleView.setText(title); messageView.setText(message); confirmButton.setOnClickListener(confirmListener); cancelButton.setOnClickListener(cancelListener); AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setView(view); return builder.create(); } } ``` 最后,在我们的Activity中,我们可以通过创建一个实例对象,并调用show方法来显示弹框。例如: ```java IOSAlertDialog dialog = new IOSAlertDialog( "提示", "确定要删除吗?", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 确定按钮点击事件 } }, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 取消按钮点击事件 } }); dialog.show(getSupportFragmentManager(), "IOSAlertDialog"); ``` 这样,我们就可以实现一个Android仿IOS自定义AlertDialog提示框了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值