单击 AutoComplateTextView 项目时隐藏键盘

Jac*_*cek -1 android autocompletetextview android-softkeyboard

我试图在用户单击 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);
        }
    });
Run Code Online (Sandbox Code Playgroud)

Kha*_*aha 5

将此代码放入 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);

    }
Run Code Online (Sandbox Code Playgroud)