在部分安卓机型遇到 SoftInputMode 设置 adjustPan / adjustResize 无效的情况。于是对键盘做了单独的监听,方便统一处理

private int oldDiff = 0;

@Override
protected void onCreate(...) {
    listenKeyboardVisible();
}

private void listenKeyboardVisible() {
    final View activityRoot = getWindow().getDecorView();
    if (activityRoot ==null) {
        return;
    }
    mainContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        private final Rect r =new Rect();
        @Override
        public void onGlobalLayout() {
            activityRoot.getWindowVisibleDisplayFrame(r);
            int diff = activityRoot.getRootView().getHeight() -r.height();
            //键盘是否弹出
            boolean isOpen = (diff > 200);
            if(diff!=oldDiff){
                Log.d("keyboard", "keyboard open: "+isOpen);
                oldDiff = diff;

                // do someting...
                
            }
        }
    });
}

  

  

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-01-09
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-07-01
  • 2022-12-23
  • 2021-08-28
  • 2021-07-23
  • 2021-10-13
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案