精确获取android软键盘高度

直接上代码:

rootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        Resources resources = MainActivity.this.getResources();
        int navigationBarResourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
        int navigationBarHeight = resources.getDimensionPixelSize(navigationBarResourceId);
        int statusBarResourceId = resources.getIdentifier("status_bar_height", "dimen", "android");
        int statusBarHeight = resources.getDimensionPixelSize(statusBarResourceId);
        int totalbarHeight = navigationBarHeight + statusBarHeight;
        Rect r = new Rect();
	rootView.getWindowVisibleDisplayFrame(r);
int screenHeight = rootView .getRootView().getHeight() ;
int heightDiff = screenHeight - (r. bottom - r. top) ; if (heightDiff > totalbarHeight) { int realKeyboardHeight = heightDiff - statusBarHeight ; Log. i( "Log" , "realKeyboardHeight = " + realKeyboardHeight) ; rootView.getViewTreeObserver().removeOnGlobalLayoutListener( this) ; } }}) ;

你可能感兴趣的:(精确获取android软键盘高度)