检测回按钮,但不要关闭对话框 [英] Detect back button but don't dismiss dialogfragment

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

问题描述

我有一个浮动对话框的对话框,包括一个用户在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服务一样),但是对话框保持可见时,我希望键盘被关闭(visibility = 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.

有什么办法吗?或者可以访问关键事件的检测是为了片段系统的目的而被完全关闭的。

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天全站免登陆