Android 按下按钮时隐藏屏幕键盘

Android 按下按钮时隐藏屏幕键盘,android,keyboard,android-softkeyboard,android-input-method,Android,Keyboard,Android Softkeyboard,Android Input Method,我想知道在用户按下按钮后如何最好地隐藏屏幕上的(软?)键盘。当用户在提交之前输入文本时,他们的键盘可能仍然可见,我想在他们提交时缩小窗口 我已经找到了这个问题的答案,但这两个答案似乎都不起作用。尽管Toast通知显示在顶部,但它们仍然可见 我的代码,为了清晰起见: InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.hid

我想知道在用户按下按钮后如何最好地隐藏屏幕上的(软?)键盘。当用户在提交之前输入文本时,他们的键盘可能仍然可见,我想在他们提交时缩小窗口

我已经找到了这个问题的答案,但这两个答案似乎都不起作用。尽管Toast通知显示在顶部,但它们仍然可见

我的代码,为了清晰起见:

        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromInputMethod(getCurrentFocus().getWindowToken(), 0);
我也尝试过:

        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromInputMethod(((EditText)findViewById(R.id.txtToWrite)).getWindowToken(), InputMethodManager.0); 

你试过InputMethodManager.HIDE吗?不总是吗

InputMethodManager inputManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
我认为您也可以尝试从EditText/SearchView中清除焦点。例如:

searchBar = (SearchView) v.findViewById(R.id.ditto_search_bar);
searchBar.clearFocus();

是的,我已经找到了,而且成功了。谢谢你的回复,我对你的迟到表示歉意。