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. EF方式增、删、改、查(基本使用)

    右击项目——添加——新建项——数据(C#)——选择ADO.NET实体数据模型——点击添加——然后根据实体数据模型向导来一步步的做. 用到的表 using System; using System.Da ...

  2. 二零一八阿里p7笔试116题

    1. junit用法,before,beforeClass,after, afterClass的执行顺序 2. 分布式锁 3. nginx的请求转发算法,如何配置根据权重转发 4. 用hashmap实 ...

  3. libssh2--ssh2实例

    #include "libssh2_config.h"#include<libssh2.h>#include<libssh2_sftp.h> 上述为所包含必 ...

  4. SNMP OID列表

    zabbix的snmp监控还没开始讲,不过先给大家列一些snmp常用的一些OID,比如cpu.内存.硬盘什么的.先了解这些,在使用snmp监控服务器. 系统参数(1.3.6.1.2.1.1) OID ...

  5. 手把手教你在CentOS上搭建Kubernetes集群

    作者:ChamPly 安装CentOS 1.安装net-tools [root@localhost ~]# yum install -y net-tools 2.关闭firewalld [root@l ...

  6. JavaScript005,语法

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  7. script标签中的async、defer属性

    Script标签是我们常用的引用js脚本的一种方式. 撸代码的时候,我们常常只写src属性,直接忽略其他属性. 最近发现了2个可以利用的属性:async.defer. 顾名思义async就是异步,在不 ...

  8. idea java快速生成返回值

    ctrl+alt+V 或者

  9. UEditor设置内容setContent()失效的解决方法

    ueditor常见用法: https://blog.csdn.net/qq_31879707/article/details/54894735#UE.Editor:setContent() UEdit ...

  10. java开发时,eclipse设置编码

    修改eclipse默认工作空间编码方式,General——Workspace——Text file encoding 修改工程编码方式,右击工程——Properties——Resource——Text ...