Android:AutoCompleteTextView隐藏软键盘

Chr*_*Geo 14 android autocompletetextview android-softkeyboard

我有一个AutoCompleteTextView,它通常在用户输入3个字母后提供建议.一旦我触摸建议列表隐藏软键盘,我想要一次.我在下面使用表格布局所做的只有在点击建议列表时才会隐藏键盘.

XML

<TableRow
    android:id="@+id/tableRow2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center" >

    <AutoCompleteTextView
        android:id="@+id/auto_insert_meds"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:ems="15"
        android:inputType="textVisiblePassword|textMultiLine"
        android:scrollHorizontally="false"
        android:text=""
        android:textSize="16sp" />
</TableRow>
Run Code Online (Sandbox Code Playgroud)

Java的

TableLayout tbl = (TableLayout) findViewById(R.id.main_table);
tbl.setOnTouchListener(new OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        in.hideSoftInputFromWindow(v.getWindowToken(), 0);
        return true;
    }
});
Run Code Online (Sandbox Code Playgroud)

用于自定义列表的XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/medlist_linear_layout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

        <TextView
        android:id="@+id/med_name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:scrollHorizontally="false"
        android:padding="3dp"
        android:textColor="@android:color/white" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

小智 21

使用OnItemClickListener.希望这工作:)

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.getWindowToken(), 0);

  }

});
Run Code Online (Sandbox Code Playgroud)

更新

用这个

in.hideSoftInputFromWindow(arg1.getApplicationWindowToken(), 0);
Run Code Online (Sandbox Code Playgroud)

代替 -

in.hideSoftInputFromWindow(arg1.getWindowToken(), 0);
Run Code Online (Sandbox Code Playgroud)

  • 只需使用... in.hideSoftInputFromWindow(arg1.getApplicationWindowToken(),0); 代替.. in.hideSoftInputFromWindow(arg1.getWindowToken(),0); 它会工作...... :) (11认同)
  • 我已经在AutoCompleteTextView上实现了OnItemClickListener,但只有在我单击列表中选择一个值时才有效,而不是当我'触摸'它以便滚动它时. (2认同)

Shy*_*dda 16

正如chakri Reddy建议:

这对我有用,我刚刚更换了

这个:

in.hideSoftInputFromWindow(arg1.getWindowToken(),0);

有:

in.hideSoftInputFromWindow(arg1.getApplicationWindowToken(),0);


nox*_*oxo 9

1)将AutoCompleteTextView下拉高度更改为MATCH_PARENT

setDropDownHeight(LinearLayout.LayoutParams.MATCH_PARENT);
Run Code Online (Sandbox Code Playgroud)

2)通过覆盖AutoCompleteTextView适配器的getView()来关闭软键盘

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View v = super.getView(position, convertView, parent);
    v.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                InputMethodManager imm = (InputMethodManager) getActivity()
                        .getSystemService(
                                Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(
                        typeTextView.getWindowToken(), 0);
            }

            return false;
        }
    });

    return v;
}
Run Code Online (Sandbox Code Playgroud)


Roa*_*nmo 7

如果您需要隐藏键盘,即使点击/单击(在带有 DropDown 菜单(TextInputLayout + MaterialAutoCompleteTextView)的下拉场景中,以防止从预定义的下拉菜单中选择时出现键盘)

(科特林)

autoCompleteTextView.inputType = InputType.TYPE_NULL
Run Code Online (Sandbox Code Playgroud)

RG


Car*_*rau 5

在类似的线程中找到了这一块代码.希望能帮助到你:

    private void hideKeyboard() {   
        // Check if no view has focus:
        View view = this.getCurrentFocus();
        if (view != null) {
            InputMethodManager inputManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
            inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
}
Run Code Online (Sandbox Code Playgroud)

链接到原始帖子.请注意,这不是公认的答案.