当键盘出现时,Facebook应用程序如何处理布局更改?

时间:2015-05-22 19:33:13

标签: android android-softkeyboard

Facebook应用程序在登录屏幕上处理UI更改时非常惊人。看看吧。

无键盘

enter image description here

使用键盘(注意如何需要帮助?英语更改视图消失)

enter image description here

是的,我确实知道 adjustPan adjustResize 设置,但Facebook如何准确设置键盘存在时哪些视图可见?

3 个答案:

答案 0 :(得分:1)

当软键盘变得可见时,似乎他们正在显示/隐藏特定文本(例如上面的示例中需要帮助或英语)。

Now, in Android, there is No direct way to detect if soft keyboard is visible or not.

为什么?

以下是Android框架工程师的答案。

  

显示的IME几乎没有意义,因为IME究竟如何   行为取决于它 - 它可能是透明的叠加而不是影响   应用程序,小条带或所有其他类型的东西。

     

因此,您与IME互动的主要方式是设置您的   softInputMode可以调整大小,所以当IME说它想要   屏蔽部分屏幕,您的应用的UI将调整大小以获取该值   如果需要考虑。

但是,有不同的方法/方法/解决方法,通过使用哪些应用程序(如Facebook)可能检测软键盘是否可见,并根据结果,应用程序可以在其UI中显示/隐藏文本/小部件。 / p>

方法1:

InputMethodManager imm = (InputMethodManager) getActivity()
            .getSystemService(Context.INPUT_METHOD_SERVICE);

    if (imm.isAcceptingText()) {
        //soft keyboard is shown, so hide "need help" text, for example
    } else {
        //Software Keyboard was not shown;
    }

方法2:

final View activityRootView = findViewById(R.id.activityRoot);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(
        new OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                int heightView = activityRootView.getHeight();
                int widthView = activityRootView.getWidth();
                if (1.0 * widthView / heightView > 3) {
                    //Make changes for Keyboard not visible
                } else {
                    //Make changes for keyboard visible
                }
            }
        });

现在上述两种方法在每种情况下都不一定100%有效(因为人们可以从SDK中获得不支持的操作方式)

Facebook可能正在使用上述任何一种方法,或者谁知道,他们可能以不同的方式实现了它!

答案 1 :(得分:0)

editText.setOnFocusChangeListener(new OnFocusChangeListener() {

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        // view/hide whatever you want depending on the hasFocus value
        // hasFocus == true -> keyboard shown
    }
});

答案 2 :(得分:-2)

edittext.post(new Runnable() {
        public void run() {
            edittext.requestFocusFromTouch();
            InputMethodManager lManager = (InputMethodManager) activity
                    .getSystemService(Context.INPUT_METHOD_SERVICE);
            lManager.showSoftInput(edittext, 0);
        }
    });