如何使用具有完成阈值的 MaterialAutoCompleteTextView
How to Use MaterialAutoCompleteTextView with Completion Threshold
我想开始在我的 Android 应用程序中使用 material 组件,但我在使用 MaterialAutoCompleteTextView
时遇到了一些麻烦。我希望它表现得像标准的 AutoCompleteTextView,而且我更喜欢 OutlinedBox
样式。此外,它应该观察 completionThreshold
属性,仅在输入三个匹配字符后才展开下拉列表。
创建轮廓框的组件有四种样式可用:
Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense.ExposedDropdownMenu
Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu
Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense
Widget.MaterialComponents.TextInputLayout.OutlinedBox
但是,前两种样式不会观察 completionThreshold
并在它们处于焦点时立即展开下拉列表。其余的似乎已损坏,可能不打算与此组件一起使用。
显然,一种选择是使用后一种样式并对其进行修改。但是对于这个相对复杂的组件来说,这最终可能会有点混乱,而且使用内置行为似乎是一种相当老套的方式。
什么是正确的方法?
<?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)
,并在 xml 中设置 app:endIconMode="none"
,在 [=13] =] 包含 AutoCompleteTextView
.
我想开始在我的 Android 应用程序中使用 material 组件,但我在使用 MaterialAutoCompleteTextView
时遇到了一些麻烦。我希望它表现得像标准的 AutoCompleteTextView,而且我更喜欢 OutlinedBox
样式。此外,它应该观察 completionThreshold
属性,仅在输入三个匹配字符后才展开下拉列表。
创建轮廓框的组件有四种样式可用:
Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense.ExposedDropdownMenu
Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu
Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense
Widget.MaterialComponents.TextInputLayout.OutlinedBox
但是,前两种样式不会观察 completionThreshold
并在它们处于焦点时立即展开下拉列表。其余的似乎已损坏,可能不打算与此组件一起使用。
显然,一种选择是使用后一种样式并对其进行修改。但是对于这个相对复杂的组件来说,这最终可能会有点混乱,而且使用内置行为似乎是一种相当老套的方式。
什么是正确的方法?
<?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)
,并在 xml 中设置 app:endIconMode="none"
,在 [=13] =] 包含 AutoCompleteTextView
.