gpt4 book ai didi

android - 如何使用具有完成阈值的 MaterialAutoCompleteTextView

转载 作者:行者123 更新时间:2023-12-04 07:58:41 25 4
gpt4 key购买 nike

我想开始在我的 Android 应用程序中使用 Material 组件,但我在使用 MaterialAutoCompleteTextView 时遇到了一些麻烦。我希望它的行为类似于标准的 AutoCompleteTextView,并且我更喜欢 OutlinedBox 样式。此外,它应该遵守 completionThreshold 属性,仅在输入三个匹配字符后才展开下拉列表。

创建轮廓框的组件有四种样式可用:Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense.ExposedDropdownMenuWidget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenuWidget.MaterialComponents.TextInputLayout.OutlinedBox.DenseWidget.MaterialComponents.TextInputLayout.OutlinedBox

但是,前两种样式不遵守 completionThreshold 并在它们处于焦点时立即展开下拉列表。其余的似乎已损坏,可能不打算与此组件一起使用。

显然,一种选择是使用后一种样式并对其进行修改。但是对于这个相对复杂的组件来说,这最终可能会有点困惑,而且使用内置行为似乎是一种相当老套的方式。

什么是正确的方法?

enter image description here

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
android:layout_margin="10dp">

<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:hint="Option 1"
app:helperText="OutlinedBox.Dense.ExposedDropdownMenu"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense.ExposedDropdownMenu">

<com.google.android.material.textfield.MaterialAutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:completionThreshold="3"
/>

</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:hint="Option 2"
app:helperText="OutlinedBox.ExposedDropdownMenu"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu">

<com.google.android.material.textfield.MaterialAutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:completionThreshold="3"
/>

</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:hint="Option 3"
app:helperText="OutlinedBox.Dense"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense">

<com.google.android.material.textfield.MaterialAutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:completionThreshold="3"
/>

</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:hint="Option 4"
app:helperText="OutlinedBox"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">

<AutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:completionThreshold="3"
/>

</com.google.android.material.textfield.TextInputLayout>

</LinearLayout>

最佳答案

请尝试在您的 Java 代码中的 AutoCompleteTextView 上使用 setThreshold(3),并在中设置 app:endIconMode="none"您的 xml,在包含 AutoCompleteTextViewTextInputLayout 中。

关于android - 如何使用具有完成阈值的 MaterialAutoCompleteTextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66577006/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com