Android 反射应用---控制Dialog dismiss

有时候不想用DialogActivity来做简单的登入界面,就直接使用Dialog来做,但是Dialog有一个问题就是,点击确认或者取消按钮后,Dialog弹框会消失.

直接上程序:

<1> : 新建Android 工程如下:

Android 反射应用---控制Dialog dismiss_第1张图片


<2> : 主要程序:

DurianMainActivity.java



package org.durian.durianalertdialogreflex;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.lang.reflect.Field;

public class DurianMainActivity extends ActionBarActivity {

    private Button mButton;

    private Context mContext;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.durian_main);

        mContext=DurianMainActivity.this;

        mButton = (Button) findViewById(R.id.button);
        mButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                checkLogin();

            }
        });

    }

    private EditText mNameEdit;
    private EditText mPassEdit;

    private void checkLogin() {

        LayoutInflater layoutInflater = LayoutInflater.from(this);
        View mLoginView = layoutInflater.inflate(R.layout.login_dialog, null);

        mNameEdit = (EditText) mLoginView.findViewById(R.id.nameedit);
        mPassEdit = (EditText) mLoginView.findViewById(R.id.passedit);

        Dialog alertDialog = new AlertDialog.Builder(DurianMainActivity.this)

                .setTitle("Login")
                .setView(mLoginView)
                .setIcon(R.mipmap.ic_launcher)
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub

                        String name = mNameEdit.getText().toString();
                        String passwd = mPassEdit.getText().toString();

                        if (name.equals("durian") && passwd.equals("123456")) {
                            try {
                                Field field = dialog.getClass().getSuperclass()
                                        .getDeclaredField("mShowing");
                                field.setAccessible(true);
                                field.set(dialog, true);

                            } catch (Exception e) {
                                e.printStackTrace();
                            }

                            Toast.makeText(mContext,"Login successfully !",Toast.LENGTH_SHORT).show();

                        } else {
                            try {
                                Field field = dialog.getClass().getSuperclass()
                                        .getDeclaredField("mShowing");
                                field.setAccessible(true);
                                field.set(dialog, false);

                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            Toast.makeText(mContext,
                                    "Login account settings fail !",
                                    Toast.LENGTH_SHORT).show();
                        }

                    }
                })
                .setNegativeButton("Cancel",
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog,
                                                int which) {
                                // TODO Auto-generated method stub

                                finish();
                            }
                        }).create();
        alertDialog.show();

    }

}

durian_mian.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="org.durian.durianalertdialogreflex.DurianMainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="dialog"/>

</RelativeLayout>

login_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/nametitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginTop="12dp"
        android:textSize="16sp"
        android:text="NickName"/>

    <EditText
        android:id="@+id/nameedit"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/passtitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginTop="12dp"
        android:textSize="16sp"
        android:text="Password"/>

    <EditText
        android:id="@+id/passedit"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />


</LinearLayout>

<3> : 源代码Dialog.java一定要看的,不看都不知道为什么:


    void dismissDialog() {
        if (mDecor == null || !mShowing) {
            return;
        }

        if (mWindow.isDestroyed()) {
            Log.e(TAG, "Tried to dismissDialog() but the Dialog's window was already destroyed!");
            return;
        }

        try {
            mWindowManager.removeView(mDecor);
        } finally {
            if (mActionMode != null) {
                mActionMode.finish();
            }
            mDecor = null;
            mWindow.closeAllPanels();
            onStop();
            mShowing = false;
            
            sendDismissMessage();
        }
    }

处理掉mShowing变量.









你可能感兴趣的:(Android 反射应用---控制Dialog dismiss)