如果是在正常的情况下我们都会这样去隐藏输入法
```
public static void showInputKeyboard(Context context , View view){
```
public static void showInputKeyboard(Context context , View view){
if (null != view){
InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.showSoftInput(view, InputMethodManager.SHOW_FORCED);
}
}
public static void hideInputKeyboard(Context context , View view){
if (null != view){
InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
但是最近项目中出现了一特殊的情况,就是当edittext在dialog中的时候!这个时候去打开输入法或者隐藏输入法就有问题!解决的办法如下:
textWaterMarkdialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
// 隐藏输入法textWaterMarkdialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}
});
textWaterMarkdialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
// 隐藏输入法
textWaterMarkdialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}
});
// 显示输入法 textWaterMarkdialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);