16

Android 自动完成功能仅在两个字母后开始。我怎样才能使它在刚刚选择该字段时出现列表?

4

6 回答 6

31

要使自动完成显示在焦点上,请添加焦点侦听器并在字段获得焦点时显示下拉列表,如下所示:

editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
  @Override
  public void onFocusChange(View view, boolean hasFocus) {
    if(hasFocus){
      editText.showDropDown();
    }
  }
});

或者,如果您不需要焦点部分,只需调用 editText.showDropDown() 即可。

于 2013-02-26T12:41:07.857 回答
9

看看setThreshold方法:

public void setThreshold (int threshold)
自:API 级别 1
指定在显示下拉列表之前用户必须在编辑框中键入的最少字符数。
当阈值小于或等于 0 时,应用阈值 1。

于 2010-11-20T22:29:51.460 回答
9

扩展 AutoCompleteTextView,覆盖 enoughToFilter() 方法和 threshold 方法,使其不会将 0 阈值替换为 1 阈值:

public class MyAutoCompleteTextView extends AutoCompleteTextView {

    private int myThreshold;

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

    public MyAutoCompleteTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public MyAutoCompleteTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public void setThreshold(int threshold) {
        if (threshold < 0) {
            threshold = 0;
        }
        myThreshold = threshold;
    }

    @Override
    public boolean enoughToFilter() {
        return getText().length() >= myThreshold;
    }

    @Override
    public int getThreshold() {
        return myThreshold;
    }

}
于 2011-12-15T22:53:24.893 回答
2

对于想要使用 SearchView 更改阈值的人,您必须使用:

SearchView.SearchAutoComplete complete = (SearchView.SearchAutoComplete)search.findViewById(R.id.search_src_text);
complete.setThreshold(0);
于 2015-07-27T20:18:55.570 回答
1

根据阈值设置,在左侧用一/两个白色字符填充适配器。

于 2010-11-20T22:28:22.837 回答
0

更改 XML 设置的替代方法:正如其他人所提到的,您需要将“自动完成阈值”设置为 1

除了@systempuntoout 提到的。

您也可以在 XML 文件中执行此操作,如图所示

<AutoCompleteTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/edittext_id"
    android:inputType="textAutoComplete"
    android:completionThreshold="1"
/>

注意这一行:android:completionThreshold="1"

于 2015-02-10T14:28:28.363 回答