AutoComplete和TextInput功能在同一EditText中吗?

Ser*_*D15 2 android android-edittext

我希望在获得焦点时EditText将其显示hint为标题,并显示一个下拉列表以使用户自动完成它。这些功能分别在TextInputEditText和上可用AutoCompleteTextView。除了EditText使用继承创建自定义方式之外,还有其他方法可以实现这两者吗?提前致谢。

Sap*_*ven 5

如果您需要新的Material Design库和Android Jetpack组件,请使用以下XML布局代码:

<com.google.android.material.textfield.TextInputLayout
    style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Example TextInputLayout">

    <androidx.appcompat.widget.AppCompatAutoCompleteTextView
        style="@style/Widget.MaterialComponents.TextInputEditText.FilledBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</com.google.android.material.textfield.TextInputLayout>
Run Code Online (Sandbox Code Playgroud)

请注意,使用此库时,您应该在TextInputLayout(而不是AppCompatAutoCompleteTextView)上设置提示。

如果你想替代概括文本字段版本,替换FilledBoxOutlinedBox在两个样式属性。

  • 谢谢,这很好!我一直在寻找这个解决方案太久了 (2认同)