Android 自定义AlertDialog退出对话框

转 https://blog.csdn.net/wkh11/article/details/53081634
在项目中很多时候会出现点击返回键出现提示对话框。

不多说了,先看效果图

直接上代码

layout布局的名字是close_program

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <RelativeLayout
        android:layout_width="275dp"
        android:layout_height="169dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:background="@drawable/tab_rectangle" >
        <TextView
            android:id="@+id/tv_prompt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="15dp"
            android:text="提示"
            android:textColor="#333333"
            android:textSize="16sp" />
        <View
            android:layout_width="match_parent"
            android:layout_height="0.5dp"
            android:layout_alignParentLeft="true"
            android:layout_marginTop="45dp"
            android:background="#25b3ff" />
        <TextView
            android:id="@+id/tv_no"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="你确定要删除吗"
            android:textColor="#333333"
            android:textSize="16sp" />
        <View
            android:id="@+id/view_liner"
            android:layout_width="match_parent"
            android:layout_height="0.5dp"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/tv_no"
            android:layout_marginTop="25dp"
            android:background="#e1e1e1" />
        <LinearLayout
            android:id="@+id/tv_cancel"
            android:layout_width="90dp"
            android:layout_height="40dp"
            android:layout_alignTop="@+id/view1"
            android:layout_marginRight="10dp"
            android:layout_marginTop="5dp"
            android:layout_toLeftOf="@+id/tv_prompt" >
            <TextView
                android:layout_width="90dp"
                android:layout_height="40dp"
                android:gravity="center"
                android:text="取消"
                android:textColor="#25b3ff"
                android:textSize="16sp" />
        </LinearLayout>
        <View
            android:id="@+id/view1"
            android:layout_width="1.5dp"
            android:layout_height="48dp"
            android:background="#e1e1e1"
            android:layout_centerInParent="true"
            android:layout_below="@+id/view_liner" />
        <LinearLayout
            android:id="@+id/tv_ok"
            android:layout_width="90dp"
            android:layout_height="40dp"
            android:layout_alignTop="@+id/view1"
            android:layout_marginTop="5dp"
            android:layout_toRightOf="@+id/tv_prompt" >
            <TextView
                android:layout_width="90dp"
                android:layout_height="40dp"
                android:gravity="center"
                android:text="确定"
                android:textColor="#25b3ff"
                android:textSize="16sp" />
        </LinearLayout>
    </RelativeLayout>
</RelativeLayout>

其中有个布局的背景是圆角矩形的设置

画圆角矩形的代码  tab_rectangle

<?xml version="1.0" encoding="utf-8"?>  
<shape xmlns:android="http://schemas.android.com/apk/res/android">    
    <solid android:color="#ffffff" />    
    <corners android:topLeftRadius="10dp"   
             android:topRightRadius="10dp"    
             android:bottomRightRadius="10dp"   
             android:bottomLeftRadius="10dp"/>    
</shape>

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();
        window.setContentView(R.layout.closeprogram);
        TextView tv = (TextView) window.findViewById(R.id.tv_no);
        tv.setText("你确定要退出吗");
        LinearLayout ok = (LinearLayout) window.findViewById(R.id.tv_ok);

//确定按钮

ok.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                exit(); // 退出应用
            }
        });

//取消按钮
        LinearLayout cancel = (LinearLayout) window.findViewById(R.id.tv_cancel);
        cancel.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                dlg.cancel();
            }
        });
    }

//关闭程序

private void exit() {
        super.finish();
        android.os.Process.killProcess(android.os.Process.myPid());
        System.exit(0);
    }

假设想改变Dialog的大小能够这样写:

	AlertDialog dialog = getCustomDialog();
dialog.show(); //一定得在show完dialog后来set属性
WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
lp.width = AnimationTest.this.getResources().getDimensionPixelSize(R.dimen.dialog_width);
lp.height = AnimationTest.this.getResources().getDimensionPixelSize(R.dimen.dialog_height);
dialog.getWindow().setAttributes(lp);

Android 自定义AlertDialog退出对话框的更多相关文章

  1. Android 自定义AlertDialog(退出提示框)

    有时候我们需要在游戏或应用中用一些符合我们样式的提示框(AlertDialog) 以下是我在开发一个小游戏中总结出来的.希望对大家有用. 先上效果图: 下面是用到的背景图或按钮的图片 经过查找资料和参 ...

  2. Android自定义扁平化对话框

    平时我们开发的大多数的Android.iOS的APP,它们的风格都是拟物化设计.如今Android 4.X.iOS 7.WP8采用的是扁平化设计,可以看出扁平化设计是未来UI设计的趋势.其实扁平化设计 ...

  3. Android实现“是否退出”对话框和“带图标的列表”对话框

    今天我们学习的内容是实现两种对话框(Dialog),第一种是询问是否退出对话框,另外一种是带图标的列表对话框,程序的执行效果是,我们点击button1的时候,弹出第一种对话框,我们点击button2的 ...

  4. Android自定义AlertDialog

    常见的一种方法: [html] view plaincopyprint? AlertDialog.Builder builder; AlertDialog alertDialog; LayoutInf ...

  5. android 开发AlertDialog.builder对话框的实现

    AndroidAPI提供了Dialog对话框控件,但google明确指出不建议开发者只是使用Dialog来创建对话框,而应该自定义对话框或者使用API中提供的Dialog的子类,如AlertDialo ...

  6. Android 自定义AlertDialog的实现

    Android默认的AlertDialog太单调,我们可以通过继承原生的Dialog来实现自定义的Dialog. 本文的自定义Dialog和原生的AlertDialog的创建方式类似,通过一个静态Bu ...

  7. android 自定义AlertDialog(一段)

    java: final AlertDialog dialog = new AlertDialog.Builder(mContext) .create(); dialog.setCancelable(f ...

  8. Android 自定义AlertDialog的写法和弹出软键盘和覆盖状态栏

    private void showMyDialog(int layoutId){ AlertDialog myDialog = new AlertDialog.Builder(context).cre ...

  9. Android 自定义格式的对话框

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAaoAAADvCAIAAAAsDwfKAAAgAElEQVR4nLy9bZhcVZUofEg0GcURBk ...

随机推荐

  1. MVC方式显示数据(数据库)

    新建实体数据模型 选择ADO.NET实体数据模型,名称改为数据库名 因为使用现有数据库,所以选择来自数据库的EF设计器,只演示所以只选择一个表,空模型可后期增加表 选择从数据库更新模型 新建数据库连接 ...

  2. IDEA 导入jar包

    项IDEA的项目中导入下载好的jar包: 在intelij IDEA 中,点击File-Project Structure,出现界面的左侧点击Modules,然后点击“+”. 然后找到你要导入的jar ...

  3. Scyther GUI 攻击输出图的解释

    1.在声明事件的安全属性的时候也就是整个过程要验证的 对象: Scythe 的安全属性  分为下面几种: Secrecy: 表示数据传输过程中是安全的,即使通过不信任的网络传也不能被攻击者获得 SKR ...

  4. nginx动静分离简单实例实现

    什么是动静分离? Nginx 动静分离简单来说就是把动态和静态请求分开,不能理解成只是将动态页面和静态页面物理分离.严格意义上说应该是动态请求和静态请求分开,可以理解成使用 nginx 处理静态页面, ...

  5. Netty搭建WebSocket服务端

    Netty服务端 1.引入依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns=& ...

  6. css引用优先级

    /***************************************css注意事项*******************************************/ 浏览器优先级:设 ...

  7. nginx中ngx_http_access_module模块

    实现基于IP的访问控制功能指令:4.1 allow允许访问指定的⽹网络或地址Syntax: allow address | CIDR | unix:| all;Default: —Context: h ...

  8. Linux卸载Django

    cd /usr/lib/python2.7/dist-packages sudo rm -rf django sudo rm Django-1.8.7.egg-info 基本命令如此,具体文件因版本有 ...

  9. jQuery获取select元素选择器练习

    jQuery获取Select选择的Text和Value:语法解释: 1. $("#select_id").change(function(){//code...}); //为Sel ...

  10. python查看文件夹下所有文件

    实现查看所有文件,重点在于文件夹下又有文件夹时怎样处理,这里通过os模块来解决. 方法一 : 通过递归实现遍历所有文件夹 import os def func(path): for i in os.l ...