如何在用户从 AutoCompleteTextView 中选择 "something" 后隐藏键盘?

How to hide keyboard after the user is choose "something" from AutoCompleteTextView?

我有一个 AutoCompleteTextView,当用户输入时他会得到键盘。之后,用户从 AutoCompleteTextView 中选择需要的内容,但键盘保持不变而不是隐藏。

这是我的 Layout.xml 代码:

 <AutoCompleteTextView
            android:id="@+id/autocompleteTextView"
            android:layout_width="300dp"
            android:layout_marginLeft="33dp"
            android:layout_height="match_parent"
            android:hint="  Choose what you need"
            android:ems="3"
            android:maxLength="30"
            android:backgroundTint="#FFFFFF"/>

这是我的 java 代码:

 mAutocompleteView.setOnItemClickListener(mAutocompleteClickListener);
 mAutocompleteView.setAdapter(mAdapter);

自动完成选择后想隐藏键盘怎么办?

感谢您的帮助。

试试这个对你有用

 InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
 imm.hideSoftInputFromWindow(autocompleteTextView.getWindowToken(), 0);

试试这个肯定对你有用

    AutoCompleteTextView text = (AutoCompleteTextView) findViewById(R.id.auto_insert_meds);

    text.setOnItemClickListener(new OnItemClickListener() {

  @Override
  public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    in.hideSoftInputFromWindow(arg1.getApplicationWindowToken(), 0);

  }

});

或者,您可以使用这个:

 AutoCompleteTextView autoText= (AutoCompleteTextView) findViewById(R.id.auto_insert_meds);

autoText.setOnItemClickListener(new OnItemClickListener() {
  @Override
  public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    View view = this.getCurrentFocus();
    if (view != null) {
        InputMethodManager inputManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
        inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);

    }
  }
});