如何在对话框中实现 onWindowFocusChange() 方法 (Android)

How to implement onWindowFocusChange() method in a Dialog (Android)

我需要使用方法 onWindowFocusChange() 关闭 AlertDialog 中的系统对话框,所以我决定扩展 AlertDialog 并实现该方法。

public class MyAlertDialog extends AlertDialog {    
    private Context context;

    protected MyAlertDialog(Context context) {
       super(context);
       this.context = context;
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
       super.onWindowFocusChanged(hasFocus);

        if (!hasFocus) {
            Intent closeDialogs = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
            context.sendBroadcast(closeDialogs);
        }
    }
}

当我从 AlertDialog.Builder 调用 create()show() 时,这些方法返回 AlertDialog 而不是 MyAlertDialog 对象和 onWindowsFocusChanged() 不执行。显然我不能将 AlertDialog 转换为 MyAlertDialog.

 AlertDialog dialog = new MyAlertDialog(this);

 MyAlertDialog.Builder builder = new MyAlertDialog.Builder(this);
 builder.setMessage(...);
 builder.setCancelable(false);
 builder.setNeutralButton(...)
 builder.show(); // Returns AlertDialog
 // dialog = builder.show(); -> dialog doesn't execute onWindowsFocusChanged()
 // dialog = (MyAlertDialog) builder.show() -> Not allowed (ClassCastException)

那么,当显示 Dialog 时,如何创建和显示 MyAlertDialog 或其他关闭系统对话框的方法?我已经查找了信息,但没有找到任何信息。

提前致谢。

尝试移动覆盖的方法:

@Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (!hasFocus) {
            Intent closeDialogs = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
            sendBroadcast(closeDialogs);
        }
    }

给 activity 本身。

并使用 AlertDialog class。每当系统对话框出现时,将通过调用 activity 内部发生的方法将其关闭,无需继承自 AlertDialog class.

要使用 onWindowFocusChanged,您必须在 xml 中创建一个带有 2 个按钮的对话框布局,创建一个对话框 class 并扩展对话框,并使用它设置对话框的内容视图.

xml 对话框 (myDialog.xml):

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

    <TextView
        android:layout_width="280dp"
        android:layout_height="50dp"
        android:layout_marginBottom="20dp"
        android:layout_marginLeft="24dp"
        android:layout_marginRight="24dp"
        android:layout_marginTop="24dp"
        android:text="Title"
        android:textColor="@color/black"
        android:textSize="22sp" />

    <EditText
        android:id="@+id/password"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:layout_marginBottom="24dp"
        android:layout_marginLeft="24dp"
        android:layout_marginRight="24dp"
        android:hint="Password"
        android:inputType="textPassword"
        android:textSize="22sp" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="52dp"
        android:gravity="end">

        <Button
            android:id="@+id/btn_no"
            android:layout_width="wrap_content"
            android:layout_height="36dp"
            android:layout_marginBottom="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginTop="8dp"
            android:background="@android:color/white"
            android:clickable="true"
            android:gravity="center"
            android:maxWidth="128dp"
            android:text="Cancel"
            android:textColor="#5DBCD2"
            android:textStyle="bold" />

        <Button
            android:id="@+id/btn_yes"
            android:layout_width="wrap_content"
            android:layout_height="36dp"
            android:layout_marginBottom="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginTop="8dp"
            android:background="@android:color/white"
            android:clickable="true"
            android:gravity="center"
            android:maxWidth="128dp"
            android:text="Login"
            android:textColor="#5DBCD2"
            android:textStyle="bold" />

    </LinearLayout>
</LinearLayout>

对话框class:

public class MyDialog extends Dialog {
    private Context context;

    public F50Dialog(Context context) {
        super(context);
        this.context = context;
        requestWindowFeature(Window.FEATURE_NO_TITLE);
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (!hasFocus) {
            Intent closeDialogs = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
            sendBroadcast(closeDialogs);
        }
    }
}

在 Activity 中创建对话框:

private void myDialog() {

        LayoutInflater li = LayoutInflater.from(this);
        View promptsView = li.inflate(R.layout.myDialog, null, false);

        final MyDialog alert = new MyDialog(this);
        final EditText input = (EditText) promptsView.findViewById(R.id.password);

        spin.setAdapter(mOperatorsAryAdapter);

        alert.setContentView(promptsView);
        alert.setCancelable(false);
        alert.setTitle("Password Required");
        alert.findViewById(R.id.btn_no).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alert.dismiss();
            }
        });
        alert.findViewById(R.id.btn_yes).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (???) {
                    alert.dismiss();
                } else {

                }
            }
        });
        alert.show();
    }

现在 onWindowFocusChanged 将触发!