单击 AutoCompleteTextView 项目时隐藏键盘

Hide keyboard when AutoComplateTextView item clicked

我试图在用户单击 AutoComplateTextView 项目时隐藏软件键盘,但它不起作用。

这是我的代码:

mAutoCompleteTextView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        /**
        *   do something
        */
            InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                inputManager.hideSoftInputFromInputMethod(mAutoCompleteTextView.getWindowToken(), 0);
        }
    });

将此代码放在 onClick 方法中:

    InputMethodManager inputManager = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);

    View v = getActivity().getCurrentFocus();

    if (v != null) {

        getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

        inputManager.hideSoftInputFromWindow(v.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);

    }
View view = this.getCurrentFocus();
if (view != null) { 
 InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
 imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}

把它放在你的点击事件中