@Override    public void onBackPressed() {    new AlertDialog.Builder(this).setTitle("确认退出吗?")        .setIcon(android.R.drawable.ic_dialog_info)        .setPositiveButton("确定", new DialogInterface.OnClickListener() {             @Override            public void onClick(DialogInterface dialog, int which) {            // 点击“确认”后的操作            MainFragmentActivity.this.finish();             }        })        .setNegativeButton("返回", new DialogInterface.OnClickListener() {             @Override            public void onClick(DialogInterface dialog, int which) {            // 点击“返回”后的操作,这里不设置没有任何操作            }        }).show();    // super.onBackPressed();    }

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

经过查找资料和参考了一下例子后才知道,要实现这种效果很简单.就是在设置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的效果了. 用同样的思路可以实现其它更复杂的效果.


更多相关文章

  1. Android中退出程序的提示框
  2. Android中如何使用对话框(单选对话框和多选对话框)
  3. Android(安卓)计时器的实现
  4. Android(安卓)双击返回键退出功能的实现
  5. Android(安卓)自定义弹出框
  6. Android按返回键退出程序
  7. Android(安卓)menu 使用初步
  8. android 按两次返回键退出
  9. Android中OptionMenu的使用

随机推荐

  1. android中的style部分属性值介绍
  2. Android抖动的动画效果
  3. 可点击价格走势图-贝塞尔曲线
  4. android 隐藏标题栏
  5. Android实际分辨率(android:anyDensity="
  6. 如何将Activity设置为Dialog样式
  7. Android之记录ListView的checkbox选择位
  8. android 内存优化 性能优化
  9. android之 JNI端获取并操作Surface
  10. Android UI开发第一篇——android的九宫