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




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM