一、页面初始化隐藏:
Manifest中增加:
android:windowSoftInputMode="adjustPan|stateHidden"/>
二、强制隐藏:
(view为接受软键盘输入的视图,SHOW_FORCED表示强制显示)
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(view,InputMethodManager.SHOW_FORCED);//强制显示
imm.hideSoftInputFromWindow(view.getWindowToken(), 0); //强制隐藏键盘
三、弹出的隐藏,隐藏的弹出
public static void hideKeyboard() {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
}
四、其它方法
public class SoftKeyboardUtil {
/**
* 隐藏软键盘(只适用于Activity,不适用于Fragment)
*/
public static void hideSoftKeyboard(Activity activity) {
View view = activity.getCurrentFocus();
if (view != null) {
InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
/**
* 隐藏软键盘(可用于Activity,Fragment)
*/
public static void hideSoftKeyboard(Context context, List viewList) {
if (viewList == null) return;
InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Activity.INPUT_METHOD_SERVICE);
for (View v : viewList) {
inputMethodManager.hideSoftInputFromWindow(v.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
}
五、获取输入法状态
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
boolean isOpen = imm.isActive();//isOpen若返回true,则表示输入法打开