软件键盘的android可视性

软件键盘的android可视性,android,android-softkeyboard,Android,Android Softkeyboard,我想检查软件键盘是否可见。我读过这个 但是activityRootView.getRootView().getHeight()和activityRootView.getHeight()始终返回相同的值,不关心键盘是否可见。你知道为什么吗?因为这个解决方案似乎对其他人有效 此代码可能会有所帮助- public void dismissKeyboard(){ InputMethodManager imm =(InputMethodManager)this.getSystemService(C

我想检查软件键盘是否可见。我读过这个

但是
activityRootView.getRootView().getHeight()
activityRootView.getHeight()
始终返回相同的值,不关心键盘是否可见。你知道为什么吗?因为这个解决方案似乎对其他人有效

此代码可能会有所帮助-

public void dismissKeyboard(){
    InputMethodManager imm =(InputMethodManager)this.getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(mSearchBox.getWindowToken(), 0);
    mKeyboardStatus = false;
}

public void showKeyboard(){
    InputMethodManager imm =(InputMethodManager)this.getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
    mKeyboardStatus = true;
}

private boolean isKeyboardActive(){
    return mKeyboardStatus;
}
mKeyboardStatus的默认布尔值将初始化为false

然后按如下方式检查值,必要时执行操作:

mSearchBox.requestFocus();
if(!isKeyboardActive()){
    showKeyboard();
}else{
    dismissKeyboard();
}
编辑-

找到答案的最简单方法-

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.20) { // 0.20 ratio is perhaps enough to determine keypad height.
    // keyboard is opened
}
else {
    // keyboard is closed
}
}
});
你喜欢这个答案吗?将此标记为选中的
请:)。

此代码可能会有所帮助-

public void dismissKeyboard(){
    InputMethodManager imm =(InputMethodManager)this.getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(mSearchBox.getWindowToken(), 0);
    mKeyboardStatus = false;
}

public void showKeyboard(){
    InputMethodManager imm =(InputMethodManager)this.getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
    mKeyboardStatus = true;
}

private boolean isKeyboardActive(){
    return mKeyboardStatus;
}
mKeyboardStatus的默认布尔值将初始化为false

然后按如下方式检查值,必要时执行操作:

mSearchBox.requestFocus();
if(!isKeyboardActive()){
    showKeyboard();
}else{
    dismissKeyboard();
}
编辑-

找到答案的最简单方法-

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.20) { // 0.20 ratio is perhaps enough to determine keypad height.
    // keyboard is opened
}
else {
    // keyboard is closed
}
}
});

你喜欢这个答案吗?将此标记为选中。
请:)。

是否将android:WindowsOfInputMode=“adjustResize”添加到您的AndroidManifest?它解决了问题,谢谢。您是否将android:WindowsOfInputMode=“adjustResize”添加到AndroidManifest?它解决了问题,谢谢。谢谢您的解决方案,它工作正常。为什么使用0.20比例?感谢您的解决方案,它工作正常。为什么使用0.20比率?