Android精准的获取底部虚拟键盘的高度

	/**
	 * 获取底部虚拟键盘的高度
	 */
	public int getBottomKeyboardHeight(){
            int screenHeight =  getAccurateScreenDpi()[1];
            DisplayMetrics dm = new DisplayMetrics();
 	    getWindowManager().getDefaultDisplay().getMetrics(dm);
            int heightDifference = screenHeight - dm.heightPixels;
            return heightDifference;
	}
	
	/**
	 * 获取精确的屏幕大小
	 */
	public int[] getAccurateScreenDpi()
        {   
	    int[] screenWH = new int[2];
            Display display = getWindowManager().getDefaultDisplay();
            DisplayMetrics dm = new DisplayMetrics();
            try {
                Class c = Class.forName("android.view.Display");
                Method method = c.getMethod("getRealMetrics",DisplayMetrics.class);
                method.invoke(display, dm);
                screenWH[0] = dm.widthPixels;
                screenWH[1] = dm.heightPixels;
             }catch(Exception e){
                e.printStackTrace();
             }  
             return screenWH;
    }


你可能感兴趣的:(android,虚拟键盘,获取虚拟键盘的高度)