質問

I'm wondering how best to hide the on-screen (soft?) keyboard after the user pushes a button. They keyboard will likely still be visible as the user is entering text before submitting it, I'd like to shrink the window on their submission.

I have found answers to the question here and here but neither seem to properly work. They keyboard is still visible, though Toast notifications are displayed on top.

My code, for clarity:

        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromInputMethod(getCurrentFocus().getWindowToken(), 0);

And I have also tried:

        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromInputMethod(((EditText)findViewById(R.id.txtToWrite)).getWindowToken(), InputMethodManager.0); 
役に立ちましたか?

解決

Have you tried InputMethodManager.HIDE_NOT_ALWAYS?

InputMethodManager inputManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);

I think you can also try clearing the focus from the EditText/SearchView too. For example:

searchBar = (SearchView) v.findViewById(R.id.ditto_search_bar);
searchBar.clearFocus();
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top