如果软件键盘在Android设备上可见,如何检测?

Android有没有办法检测软件(又名“软”)键盘是否在屏幕上可见?


没有直接的方法 - 请参阅http://groups.google.com/group/android-platform/browse_thread/thread/1728f26f2334c060/5e4910f0d9eb898a其中Android团队的Dianne Hackborn已回复。 但是,您可以通过检查#onMeasure中的窗口大小是否更改来间接检测它。 请参阅如何检查Android中软件键盘的可见性?


这对我有用。 也许这对所有版本来说都是最好的方式。

contentView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {

    Rect r = new Rect();
    contentView.getWindowVisibleDisplayFrame(r);
    int screenHeight = contentView.getRootView().getHeight();

    // r.bottom is the position above soft keypad or device button.
    // if keypad is shown, the r.bottom is smaller than that before.
    int keypadHeight = screenHeight - r.bottom;

    Log.d(TAG, "keypadHeight = " + keypadHeight);

    if (keypadHeight > screenHeight * 0.15) { // 0.15 ratio is perhaps enough to determine keypad height.
        // keyboard is opened
    }
    else {
        // keyboard is closed
    }
}
});

尝试这个:

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

    if (imm.isAcceptingText()) {
        writeToLog("Software Keyboard was shown");
    } else {
        writeToLog("Software Keyboard was not shown");
    }
链接地址: http://www.djcxy.com/p/93351.html

上一篇: How do I Detect if Software Keyboard is Visible on Android Device?

下一篇: Hide soft keyboard when an EditText field receives focus