如何知道键盘何时最小化?

时间:2014-04-28 10:23:38

标签: android android-edittext android-softkeyboard keyevent

我希望隐藏光标并在键盘最小化后立即执行其他操作。我已经启用光标并在触摸editext时显示键盘,但是我找不到方法知道用户何时最小化它而不按“完成”。

有办法吗?

3 个答案:

答案 0 :(得分:0)

尝试以下代码: -

activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(
            new OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    Rect r = new Rect();
                    activityRootView.getWindowVisibleDisplayFrame(r);

                    int heightDiff = activityRootView.getRootView()
                            .getHeight() - (r.bottom - r.top);
                    if (lastDiff == heightDiff)
                        return;
                    lastDiff = heightDiff;
                    Log.i("aerfin","arefin "+lastDiff);
                    if (heightDiff > 100) { // if more than 100 pixels, its
                                            // probably a keyboard...
                        flag2 = 0;
                    } else {
                        if (flag == false)
                            flag2 = 1;
                    }
                }
            });

更多信息请参阅以下链接最少使用全部为我: -

Maintain keyboard open/closed state for EditText when app comes to foreground

How to check visibility of software keyboard in Android?

答案 1 :(得分:0)

在您的清单中,通过将此行作为参数放在清单中的<activity>标记中,确保键盘显示或隐藏时配置发生更改:

android:configChanges="orientation|keyboardHidden"

然后在Activity

中覆盖此方法
@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    // Checks whether a hardware keyboard is available
    if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO) {
        Toast.makeText(this, "keyboard visible", Toast.LENGTH_SHORT).show();
    } else if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_YES) {
        Toast.makeText(this, "keyboard hidden", Toast.LENGTH_SHORT).show();
    }
}

答案 2 :(得分:-1)

InputMethodManager imm = (InputMethodManager)getSystemService(
      Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);

//to show soft keyboard
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);

//to hide it, call the method again
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);