• Android 自定义AlertDialog退出对话框


    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);



  • 相关阅读:
    水晶报表 VS2010 应用
    crystalreport使用方法
    spring MVC核心思想
    一台服务器配置多个TOMCAT
    SQL server索引
    锁机制
    JAVA书籍
    Live 直播过程
    html5 video微信浏览器视频不能自动播放
    设计模式 抽象工厂模式
  • 原文地址:https://www.cnblogs.com/it-tsz/p/11220098.html
Copyright © 2020-2023  润新知