fragment中:
import android.view.inputmethod.InputMethodManager;
private void hideSoftInput() {
View v = getActivity().getCurrentFocus();
if (v != null && v.getWindowToken() != null) {
InputMethodManager manager = (InputMethodManager) getContext()
.getSystemService(Context.INPUT_METHOD_SERVICE);
boolean isOpen = manager.isActive();
if (isOpen) {
manager.hideSoftInputFromWindow(v.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
}
activity中
private void hideSoftInput() {
View view = getCurrentFocus();
if (view != null) {
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}