/** * 判斷鍵盤是否顯示 * * @return true 顯示 */public static boolean isSoftShowing(Activity activity) { //獲取當前屏幕內容的高度 int screenHeight = activity.getWindow().ge ...
/**
* 判斷鍵盤是否顯示
*
* @return true 顯示
*/
public static boolean isSoftShowing(Activity activity) {
//獲取當前屏幕內容的高度
int screenHeight = activity.getWindow().getDecorView().getHeight();
//獲取View可見區域的bottom
Rect rect = new Rect();
activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
return screenHeight - rect.bottom != 0;
}
/**
* 隱藏鍵盤
*/
public static void hide(Activity activity) {
if (isSoftShowing(activity)){
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
}
}