int mNavSize;publicintgetSoftInputHeight(){int mSoftInputHeight;int mScreenHeight = ScreenUtils.getScreenHeight(this);final View mDecorView =this.getWindow().getDecorView();
Rect rect =newRect();// Window可视区域座标(不包含状态栏与底部导航栏高度,此时软键盘已经升起)
mDecorView.getWindowVisibleDisplayFrame(rect);// 不建议用这种方式获取软键盘高度// int mDecorViewBottom = mDecorView.getBottom();// int mSoftInputHeight = Math.abs(mDecorViewBottom - rect.bottom);int diffSize = Math.abs(mScreenHeight - rect.bottom);if(mNavSize >0){// 如果导航栏可见
mSoftInputHeight = diffSize - mNavSize;}else{// 如果导航栏不可见
mSoftInputHeight = diffSize;}return mSoftInputHeight;}
屏幕工具类
publicfinalclassScreenUtils{/**
* Return the width of screen, in pixel.
*
* @return the width of screen, in pixel
*/publicstaticintgetScreenWidth(Context context){
WindowManager wm =(WindowManager) context.getSystemService(Context.WINDOW_SERVICE);if(wm == null)return-1;
Point point =newPoint();if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1){
wm.getDefaultDisplay().getRealSize(point);}else{
wm.getDefaultDisplay().getSize(point);}return point.x;}/**
* Return the height of screen, in pixel.
*
* @return the height of screen, in pixel
*/publicstaticintgetScreenHeight(Context context){
WindowManager wm =(WindowManager) context.getSystemService(Context.WINDOW_SERVICE);if(wm == null)return-1;
Point point =newPoint();if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1){
wm.getDefaultDisplay().getRealSize(point);}else{
wm.getDefaultDisplay().getSize(point);}return point.y;}}