监听软键盘显示并且整体向上移动

private void listenerSoftInput() {
    final View activityRootView = findViewById(R.id.loginScroll);
    activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(
        new ViewTreeObserver.OnGlobalLayoutListener() {
      @Override
     public void onGlobalLayout() {
           int heightDiff = activityRootView.getRootView().getHeight() - activityRootView.getHeight();
           if (heightDiff > 50) { // 如果高度差超过100像素,就很有可能是有软键盘...
               scrollToTop();
             } else {
        }
        }
    });
}

//scrollview滑到顶部

protected void scrollToTop() {
    mHandler.postDelayed(new Runnable() {
        @Override
        public void run() {
            loginScroll.scrollTo(10000,10000);
        }
    }, 100);

}

你可能感兴趣的:(监听软键盘显示并且整体向上移动)