android - 将 AutoCompleteTextView 限制为仅一个结果

标签 android autocompletetextview

我正在使用 AutoCompleteTextView 和适配器的自定义布局。问题是我不知道如何一次限制结果只有一个,就像在默认布局中一样。 我读到它可以限制高度,但不适用于所有屏幕。感谢您的关注。

我的 Activity_main 布局上有这个。

<AutoCompleteTextView android:id="@+id/autotext" 
android:layout_width="0dp" 
android:layout_height="wrap_content"
android:minWidth="480dp" 
android:layout_weight="1" 
android:maxLength="23" 
android:maxLines="1" 
android:textColor="#000000" />

这是适配器的布局。

<TextView android:id="@+id/textpop" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content"
android:textColor="#000000"
android:textSize="17sp"/>

最佳答案

看看this code 。它基本上是 SDK ArrayAdapter 类的代码,稍作修改(这不算)。如果您只想在 AutoCompleteTextView 中显示一项建议,那么您可以从链接到 performFiltering() 方法对类进行一些小修改,如下所示:

// ... the rest of that method
if (newValues.size() > 1) { // if we have more than an item in the list
    // get the first suggestion
    T oneItem = newValues.get(0);
    // empty the suggestion list
    newValues.clear();
    // add the only suggestion.
    newValues.add(oneItem);
}
results.values = newValues;
results.count = newValues.size();

关于android - 将 AutoCompleteTextView 限制为仅一个结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13688904/

相关文章:

android - 编译失败并出现神秘错误

android - 通过 Google Play 分发 Android 应用程序并直接作为 .apk 文件

android - 有没有办法在 flutter 中为 DropdownButton 添加标签?

android - 如何使用 Intent 限制录音时间?

android - AutoCompleteTextView 右侧的加载指示器

android - 如何在填充有数组的 AutoCompletetextview 中找到项目的位置

Android AWS Amplify - 库尝试调用用户回调两次,预计只有一次

android - 在 AutocompleteTextView 中显示所有项目而不写入文本

Android - 自定义 AutoCompleteTextView CursorAdaptor - 建议行为

android - 如何将加载指示器添加到 AutoCompleteTextView