检测后退按钮但不关闭对话框片段 [英] Detect back button but don't dismiss dialogfragment

查看:20
本文介绍了检测后退按钮但不关闭对话框片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个浮动对话框的对话框片段,其中包括一个特殊键盘,当用户在 EditText 字段内按下时会弹出一个特殊键盘(正常的 IME 停止显示).

I have a dialogfragment for a floating dialog which includes a special keyboard that pops up when a user presses inside an EditText field (the normal IME is stopped from being displayed).

我希望在用户按下后退按钮(就像使用普通 IME 服务一样)时关闭键盘(可见性 = GONE),但对话框保持可见.但是,从我对 SO 和其他地方的相当广泛的阅读中可以看出,似乎没有办法做到这一点.

I would like the keyboard to be dismissed (visibility = GONE) when the user presses the back button (just as with a normal IME service) but the dialog to remain visible. However, there does not appear to be a way to do this as far as I can see from my fairly extensive reading on SO and elsewhere.

如果我将对话框设置为不可取消,则 onCancel() 或 onDismiss() 不会通知我,因为对话框不可取消.

If I set the dialog to be non-cancelable then I don't get notified by onCancel() or onDismiss() because the dialog isn't cancelable.

如果我将对话框设置为可取消,我会收到通知,但对话框会被关闭.

If I set the dialog to be cancelable then I get notified, but the dialog is dismissed.

我无法将 onKeyListener 附加到片段中的对话框,因为它已被系统替换,以便片段可以处理对话框的生命周期.

I can't attach an onKeyListener to the dialog in the fragment because it is replaced by the system so that the fragment can handle the dialog's life cycle.

有没有办法做到这一点?或者是否为了 Fragment 系统的目的而完全隔离了对关键事件的检测?

Is there any way to do this? Or has access to the detection of key events been entirely fenced off for the purposes of the Fragment system?

推荐答案

最好和最干净的方法是在你在 onCreateDialog() 中创建的对话框中覆盖 onBackPressed().

The best way and cleanest way is to override onBackPressed() in the dialog you created in onCreateDialog().

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    return new Dialog(getActivity(), getTheme()){
        @Override
        public void onBackPressed() {
            //do your stuff
        }
    };
}

这篇关于检测后退按钮但不关闭对话框片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆