立即在 Android 上显示自动完成功能

发布于 2024-10-03 04:51:34 字数 48 浏览 4 评论 0原文

Android 自动完成功能仅在两个字母后启动。如何才能在刚刚选择字段时显示列表?

The Android autocomplete only starts after two letters. How can I make it so the list appears when the field is just selected?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

许你一世情深 2024-10-10 04:51:34

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

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

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

To get the autocomplete to show on focus add focus listener and show the drop down when the field gets focus, like this:

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

Or just call editText.showDropDown() if you don't need the focus part.

柒夜笙歌凉 2024-10-10 04:51:34

查看 setThreshold 方法:

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

Have a look to setThreshold method:

public void setThreshold (int
threshold)
Since: API Level 1
Specifies the minimum number of
characters the user has to type in the
edit box before the drop down list is
shown.
When threshold is less than or equals 0, a threshold of 1 is applied.

神经大条 2024-10-10 04:51:34

扩展 AutoCompleteTextView,重写 fullToFilter() 方法和阈值方法,以便它不会用 1 阈值替换 0 阈值:

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;
    }

}

Extend the AutoCompleteTextView, overriding the enoughToFilter() methods and the threshold methods so that it doesn't replace the 0 threshold with a 1 threshold:

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;
    }

}
一杯敬自由 2024-10-10 04:51:34

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

SearchView.SearchAutoComplete complete = (SearchView.SearchAutoComplete)search.findViewById(R.id.search_src_text);
complete.setThreshold(0);

For people who want to change threshold using SearchView you have to use:

SearchView.SearchAutoComplete complete = (SearchView.SearchAutoComplete)search.findViewById(R.id.search_src_text);
complete.setThreshold(0);
兲鉂ぱ嘚淚 2024-10-10 04:51:34

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

Pad your adapter with one/two white character on left depending on the threshold setting.

⒈起吃苦の倖褔 2024-10-10 04:51:34

更改 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"

Alternate method of changing the setting in your XML:
As mentioned by others you need to set your 'Auto Completion Threshold' to 1

Other than what @systempuntoout mentioned.

You can also do that in your XML file as shown

<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"
/>

Note the line : android:completionThreshold="1"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文