在不写文本的情况下显示AutocompleteTextView中的所有项目

and*_*ter 45 android autocompletetextview

我有一个AutocompleteTextView,它工作正常.当我写一个单词时,它会显示相关的结果,但我想显示所有项目而不在AutocompleteTextView中写任何单词.我怎样才能做到这一点.

Tal*_*lha 76

您需要扩展AutoCompleteTextView,

"当阈值小于或等于0时,应用阈值1.".

setThreshold

import android.content.Context;  
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.AutoCompleteTextView;

public class InstantAutoComplete extends AutoCompleteTextView {

    public InstantAutoComplete(Context context) {
        super(context);
    }

    public InstantAutoComplete(Context arg0, AttributeSet arg1) {
        super(arg0, arg1);
    }

    public InstantAutoComplete(Context arg0, AttributeSet arg1, int arg2) {
        super(arg0, arg1, arg2);
    }

    @Override
    public boolean enoughToFilter() {
        return true;
    }

    @Override
    protected void onFocusChanged(boolean focused, int direction,
            Rect previouslyFocusedRect) {
        super.onFocusChanged(focused, direction, previouslyFocusedRect);
       if (focused && getFilter()!=null) {
        performFiltering(getText(), 0);
    }
    }

}
Run Code Online (Sandbox Code Playgroud)

在xml中

<AutoCompleteTextView ... /> to <your.namespace.InstantAutoComplete ... />
Run Code Online (Sandbox Code Playgroud)

编辑1

创建名为InstantAutoComplete的新类,然后将此代码放入类中.

在你的布局中,xml使用这个类

然后在你的actity中找到这个小部件(onCreate方法).

看看这个例子

  • 我在com.mybusiness.ui.view.InstantAutoComplete.onFocusChanged(InstantAutoComplete.java:35)的android.widget.AutoCompleteTextView.performFiltering(AutoCompleteTextView.java:841)上获得java.lang.NullPointerException.有什么建议? (7认同)

Mel*_*pes 40

这里更好的解决方案

您无需自定义AutoCompleteTextView.相反,autoCompleteTextView.showDropDown()只要你需要它就打电话.....欢呼:)

  • 这只会第一次工作.但是如果你写了一些然后清除它,它就什么都不会显示出来.这是因为您尝试显示适配器的当前状态的弹出窗口,该字符串在使用空字符串过滤后为空. (3认同)
  • 当focus = true时,你应该在OnFocusChangeListener中使用showDropDown(),而在OnTextChanged中,当length = 0时,你应该使用showDropDown(); (3认同)
  • 不幸的是,这并不总是有效。我不确定为什么,但在某些设备上,当文本字段为空时似乎忽略了该命令。 (2认同)

小智 26

这个对我有用:

添加到您的对象下一个事件方法:

    myView.setOnFocusChangeListener(new OnFocusChangeListener() {

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus)
                myView.showDropDown();

        }
    });

    myView.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            myView.showDropDown();
            return false;
        }
    });
Run Code Online (Sandbox Code Playgroud)

  • 我不认为你需要两者。不触摸 View 也给它焦点吗? (2认同)

Ho *_*ong 12

这对我来说非常有用,这是解决问题的简单方法:

final ArrayAdapter<String> adapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_dropdown_item_1line, usernameLists);
etUsername.setThreshold(1);
etUsername.setAdapter(adapter);
etUsername.setOnTouchListener(new View.OnTouchListener() {

    @SuppressLint("ClickableViewAccessibility")
    @Override
    public boolean onTouch(View paramView, MotionEvent paramMotionEvent) {
        if (usernameLists.size() > 0) {
                // show all suggestions
                if (!etUsername.getText().toString().equals(""))
                    adapter.getFilter().filter(null);
                etUsername.showDropDown();
            }
        return false;
    }
});
Run Code Online (Sandbox Code Playgroud)


Hem*_*ori 8

您需要调用requestFocus();以显示键盘,否则键盘不会弹出。

该方法强制显示下拉列表。

autocomptv.setOnTouchListener(new OnTouchListener() {

        @SuppressLint("ClickableViewAccessibility")
        @Override
        public boolean onTouch(View paramView, MotionEvent paramMotionEvent) {
            // TODO Auto-generated method stub
            autocomptv.showDropDown();
            autocomptv.requestFocus();
            return false;
        }
    });
Run Code Online (Sandbox Code Playgroud)


Abd*_*ery 6

你需要把这些步骤让它完美地工作

1-在你的 xml 中放这个

    <androidx.appcompat.widget.AppCompatAutoCompleteTextView
            android:id="@+id/account_type_spinner"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/_16sdp"
            android:layout_marginTop="@dimen/_24sdp"
            android:layout_marginEnd="@dimen/_16sdp"
            android:background="@drawable/rounded_edt_with_border"
            android:completionThreshold="0"
            android:drawableRight="@drawable/ic_arrow_down"
            android:hint="@string/account_type"
            android:imeOptions="actionNext"
            android:inputType="text"
            android:padding="12dp"
            android:textSize="@dimen/_15sp"
             />
Run Code Online (Sandbox Code Playgroud)

你只需要设置android:completionThreshold为零

2-在你的java代码中放入

  mViewDataBinding.accountTypeSpinner.setOnFocusChangeListener((v, hasFocus) -> {
        if (hasFocus)
            mViewDataBinding.accountTypeSpinner.showDropDown();
    });
Run Code Online (Sandbox Code Playgroud)