XAML Android上的可搜索下拉菜单

wi3ka0sx  于 2023-08-01  发布在  Android
关注(0)|答案(1)|浏览(129)

我需要创建具有搜索功能的下拉菜单。我的目标是允许用户从相当大的预定义值列表中轻松地进行选择。我发现带有自动完成文本视图的文本输入布局非常好和简单。
但我缺少两个功能:a)我不想允许用户填充自定义值。他只能从预定义的值中选择。这可以通过将android:inputType="none"添加到AutoCompleteTextView来完成,但在此之后,搜索功能将消失。B)如果能够搜索包含用户输入的值,而不仅仅是以用户输入开头的值,那就太好了。例如:写上“土地”,得到“波兰”。
是否可以使用AutocompleteTextView实现此功能?还是有什么简单的替代方案?

  • 谢谢-谢谢
    这是我的自动完成文本视图的实现:
    XAML语言
<com.google.android.material.textfield.TextInputLayout
    style="@style/Widget.Material3.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Country">

    <AutoCompleteTextView
        android:id="@+id/actCountries"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </AutoCompleteTextView>
</com.google.android.material.textfield.TextInputLayout>

字符串
片段

val countries = arrayListOf<String>("Czech Republic","Slovakia", "Poland", "Germany","Austria")
val countriesAdapter = ArrayAdapter<String>(requireContext(), R.layout.support_simple_spinner_dropdown_item, countries)
binding?.actCountries?.setAdapter(countriesAdapter )

igetnqfo

igetnqfo1#

另一种方法是使用TextWatcher。您可以使用countries列表上的.filter扩展来更新onTextChanged回调中的下拉列表。

相关问题