首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用具有完成阈值的MaterialAutoCompleteTextView

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

Stack Overflow用户
提问于 2021-03-11 05:34:29
回答 1查看 1.9K关注 0票数 3

我想在我的安卓应用程序中开始使用材料组件,我在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并展开下拉列表。其余的部分似乎已经损坏,很可能不适合与此组件一起使用。

显然,一种选择是使用后一种样式并对其进行修改。但是对于这个相对复杂的组件来说,这可能会变得有点混乱,而且使用内置的行为似乎是一种相当麻烦的方法。

什么是正确的方法?

代码语言:javascript
复制
<?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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-03-11 06:02:04

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66577006

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档