Android中監聽軟鍵盤顯示狀態實現程式碼

NO IMAGE

/**監聽軟鍵盤狀態
* @param activity
* @param listener
*/
public static void addOnSoftKeyBoardVisibleListener(Activity activity, final OnSoftKeyBoardVisibleListener listener) {
final View decorView = activity.getWindow().getDecorView();
decorView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Rect rect = new Rect();
decorView.getWindowVisibleDisplayFrame(rect);
int displayHight = rect.bottom - rect.top;
int hight = decorView.getHeight();
boolean visible = (double) displayHight / hight < 0.8;
Log.d(TAG, "DecorView display hight = "   displayHight);
Log.d(TAG, "DecorView hight = "   hight);
Log.d(TAG, "softkeyboard visible = "   visible);
if(visible != sLastVisiable){
listener.onSoftKeyBoardVisible(visible);
}
sLastVisiable = visible;
}
});
}

您可能感興趣的文章:

解析android中隱藏與顯示軟鍵盤及不自動彈出鍵盤的實現方法Android 顯示和隱藏軟鍵盤的方法(手動)Android 設定Edittext獲取焦點並彈出軟鍵盤Android製作漂亮自適佈局鍵盤的方法Android鍵盤顯示與隱藏程式碼Android實現彈出鍵盤的方法Android 軟鍵盤彈出時把原來佈局頂上去的解決方法Android鍵盤輸入語言設定預設開啟myanmar緬甸語的步驟Android軟鍵盤遮擋的四種完美解決方案Android自定義控制元件之電話撥打小鍵盤