简体   繁体   English

对话框打开时检测后退按钮事件

[英]Detect Back Button Event when Dialog is Open

I'm building an app that has 2 dialogues that open up and I want something to occur if the user presses the back button while certain dialogues are open. 我正在构建一个具有2个打开的对话框的应用程序,我希望如果某些对话框打开时用户按下“后退”按钮,则会发生某些事情。 However, for some reason, the back button event is not registering when the dialogues are open. 但是,由于某些原因,当对话框打开时,后退按钮事件未注册。 I tested it by putting a log in onBackPressed() and whenever the dialogues are NOT open and I'm simply on the main activity, the logs appear on logcat. 我通过在onBackPressed()中放置一个日志进行了测试,并且只要对话框未打开并且我只是在主要活动上,日志就会显示在logcat上。 However, if the dialogues are open, I simply get this: 但是,如果对话是开放的,我将得到以下信息:

W/InputEventReceiver: Attempted to finish an input event but the input event receiver has already been disposed. W / InputEventReceiver:尝试完成输入事件,但输入事件接收器已被处置。

Below I have placed the code for the dialogues: 下面,我放置了对话的代码:

    public void pair() {
        final Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();

        AlertDialog.Builder pairedList = new AlertDialog.Builder(this);
        pairedList.setTitle("Paired Devices");

        final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.select_dialog_singlechoice);
        if (pairedDevices.size() > 0) {
            for (BluetoothDevice device : pairedDevices) {
                arrayAdapter.add(device.getName());
            }
        }

        pairedList.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                mBluetoothAdapter.disable();
//                pair_dialog = false;
            }
        });

        pairedList.setPositiveButton("Pair New", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                startActivityForResult(new Intent(android.provider.Settings.ACTION_BLUETOOTH_SETTINGS), 0);
            }
        });

        pairedList.setAdapter(arrayAdapter, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
//                connect_dialog = true;
                String strName = arrayAdapter.getItem(which);
                AlertDialog.Builder builderInner = new AlertDialog.Builder(MainActivity.this);
                builderInner.setMessage(strName);
                builderInner.setTitle("Connect To:");
                builderInner.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        for (BluetoothDevice device : pairedDevices) {
                            if(device.getName().equals(strName)){
                                paired = device;
                                dialog.dismiss();
                            }
                        }
                    }
                });
                builderInner.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
//                        connect_dialog = false;
                        pairedList.show();
                    }
                });
                builderInner.show();
            }
        });
        pairedList.show();
//        pair_dialog = true;
    }

Below is my onBackPressed() method which is right after the above method. 下面是我的onBackPressed()方法,它紧接在上述方法之后。 Nothing out of the ordinary, I don't think. 我认为没有什么不同寻常的。

   @Override
    public void onBackPressed() {
        Log.e(TAG, "Back Button Pressed");
        super.onBackPressed();
    }

Like I said, if the dialogues are not open, the log shows up just fine in logcat but if the dialogues are open, it's like the back button doesn't register. 就像我说的那样,如果对话框未打开,则日志在logcat中显示就很好,但是如果对话框打开,就好像没有注册“后退”按钮。

this worked for me... 这对我有用...

       yuordialog.setOnKeyListener(new Dialog.OnKeyListener() {

            @Override
            public boolean onKey(DialogInterface arg0, int keyCode,
                                 KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                   //your stuff....
                }
                return true;
            }
        });

If you have added, 如果您添加了,

dialog.setCancelable(false);

change it to, 改成

dialog.setCancelable(true);

Actually, setCancelable(false) cancel the event of touch outside the dialog and back press also. 实际上,setCancelable(false)会取消对话框外的触摸事件,并同时按回。

You can also use 您也可以使用

builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
    @Override
    public void onDismiss(DialogInterface dialogInterface) {
        //your dismiss code here
    }
});

This listens to both backpress events and dismiss by touch. 这将侦听backpress事件并通过触摸消除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM