自認為這是目前計算鍵盤高度中李剖,比較好的方法示绊,因此特別做些記錄,分享給大家
et.getViewTreeObserver().addOnGlobalLayoutListener(newOnGlobalLayoutListener(){
//當(dāng)鍵盤彈出隱藏的時候會 調(diào)用此方法暂论。@OverridepublicvoidonGlobalLayout() {
Rect r=newRect();//獲取當(dāng)前界面可視部分MainActivity.this.getWindow().getDecorView().getWindowVisibleDisplayFrame(r);//獲取屏幕的高度intscreenHeight =? MainActivity.this.getWindow().getDecorView().getRootView().getHeight();//此處就是用來獲取鍵盤的高度的面褐, 在鍵盤沒有彈出的時候 此高度為0 鍵盤彈出的時候為一個正數(shù)intheightDifference = screenHeight -r.bottom;
Log.d("Keyboard Size", "Size: " +heightDifference);
}
});