隐藏键盘

/**
* 判断键盘是否显示
*
* @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);
}
}
原文地址:https://www.cnblogs.com/haoziCute/p/10640851.html