检测后退按钮,但不排除dialogfragment [英] Detect back button but don't dismiss dialogfragment

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

问题描述

我有一个dial​​ogfragment的浮动对话框,其中包括一个特殊的键盘,当一个EditText领域内的用户presses(正常的输入法是从显示停止)弹出。

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).

我想键盘被解雇(知名度= GONE)当用户presses后退按钮(就像使用普通的IME服务),但该对话框仍然可见。然而,似乎没有成为一个办法做到这一点,据我可以从我相当广泛,可以参阅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?

推荐答案

我有同样的问题,而不是你,我已经固定它附着onKeyListener到dialogfragment。

I had the same problem than you and I've fixed it attaching the onKeyListener to the dialogfragment.

在方法 onResume()扩展DialogFragment的把这些片code类:

In the method onResume() of the class that extend of DialogFragment put these piece of code:

    getDialog().setOnKeyListener(new OnKeyListener()
    {
        @Override
        public boolean onKey(android.content.DialogInterface dialog, int keyCode,android.view.KeyEvent event) {

            if ((keyCode ==  android.view.KeyEvent.KEYCODE_BACK))
                {
                     //Hide your keyboard here!!!
                     return true; // pretend we've processed it
                }
            else 
                return false; // pass on to be processed as normal
        }
    });

在这里,你可以找到一个问题是这样的code将要执行两次:一是当用户preSS塔后退按钮和另一个当他离开preSS它。在这种情况下,你必须通过事件来筛选:

Here one of the problems that you can find is this code is going to be executed twice: one when the user press tha back button and another one when he leave to press it. In that case, you have to filter by event:

@Override
public void onResume() {
    super.onResume();

    getDialog().setOnKeyListener(new OnKeyListener()
    {
        @Override
        public boolean onKey(android.content.DialogInterface dialog, int keyCode,
                android.view.KeyEvent event) {

            if ((keyCode ==  android.view.KeyEvent.KEYCODE_BACK))
            {
                //This is the filter
                if (event.getAction()!=KeyEvent.ACTION_DOWN)
                        return true;
                else
                {
                    //Hide your keyboard here!!!!!!
                    return true; // pretend we've processed it
                }
            } 
            else 
                return false; // pass on to be processed as normal
        }
    });
}

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

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