我的主要活动中有一个BottomSheetBehavior,可以不断显示底部抽屉。
当我打开然后关闭键盘时(按下EditText然后按Back),“OnSlide”方法被触发:
mBottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
@Override
public void onSlide(@NonNull View bottomSheet, float slideOffset)
{
// my code
}
}
我在此功能中有代码,当用户用手指上下滑动抽屉时,该代码会运行。键盘出现和退出时,我不希望运行此代码。它被调用是没有意义的。如何防止键盘更改在此函数中运行代码?
我试过了:
android:windowSoftInputMode="adjustResize"
中更改为android:windowSoftInputMode="adjustNothing"
,这实际上符合我的要求,但删除了我添加的有用功能(在键盘启动时启用滚动功能)。键盘更改侦听器:
mContentView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener()
{
@Override
public void onGlobalLayout()
{
Rect r = new Rect();
mContentView.getWindowVisibleDisplayFrame(r);
int screenHeight = mContentView.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;
// 0.15 ratio is perhaps enough to determine keypad height.
mKeyboardIsOpen = keypadHeight > screenHeight * 0.15;
}
});