Android 打开和隐藏软键盘

时间:2022-08-31 23:40:57

开发中经常会用到关健软键盘、

把它写到一个常用工具类里面

用到时调用即可。


/** 隐藏软键盘 **/

public static void closeInputMethod(Context context) {
View view = ((Activity) context).getWindow().peekDecorView();
if (view != null) {
InputMethodManager inputmanger = (InputMethodManager) context
.getSystemService(Context.INPUT_METHOD_SERVICE);
inputmanger.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}


/** 打开软键盘 **/
public static void openInputMethod(Activity context) {
View view = context.getWindow().peekDecorView();
if (view != null) {
InputMethodManager inputmanger = (InputMethodManager) context
.getSystemService(Context.INPUT_METHOD_SERVICE);
inputmanger.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
}
}