打开/关闭键盘触发BottomSheetBehavior的OnSlide回调

时间:2017-10-09 14:41:12

标签: android android-softkeyboard bottom-sheet

我的主要活动中有一个BottomSheetBehavior,可以不断显示底部抽屉。

当我打开然后关闭键盘时(按下EditText然后按Back),“OnSlide”方法被触发:

mBottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
  @Override
  public void onSlide(@NonNull View bottomSheet, float slideOffset)
  {
    // my code
  }
}

我在此功能中有代码,当用户用手指上下滑动抽屉时,该代码会运行。键盘出现和退出时,我不希望运行此代码。它被调用是没有意义的。如何防止键盘更改在此函数中运行代码?

我试过了:

  1. 在我的AndroidManifest android:windowSoftInputMode="adjustResize"中更改为android:windowSoftInputMode="adjustNothing",这实际上符合我的要求,但删除了我添加的有用功能(在键盘启动时启用滚动功能)。
  2. 聆听软键盘更改只会在未发生更改时执行代码,因为在键盘完全隐藏后调用OnSlide(请参阅下面的代码)。
  3. 键盘更改侦听器:

    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;
        }
    });
    

0 个答案:

没有答案
相关问题