带有 AutoCompleteTextView 的 TextInputLayout 的默认文本

Cos*_*Dev 4 android autocompletetextview android-textinputlayout material-components-android

我正在使用它来显示一种Spinner视图类型TextInputLayout,但我不知道如何为其设置任何默认值。

<com.google.android.material.textfield.TextInputLayout
    style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox.ExposedDropdownMenu"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:hintTextColor="@color/primary_color_3">

    <AutoCompleteTextView
        android:id="@+id/accType_dropdown"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:backgroundTint="@android:color/transparent"
        android:inputType="none"
        android:text="@={viewModel.mytext}"
        android:lineSpacingExtra="5sp"
        android:textColor="@color/name_primary_color"
        android:textSize="14sp" />

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

占位符仅在处于焦点模式时有效,因此请提出解决方法。

编辑:

我在此文本视图中使用两种方式的数据绑定,因此似乎没有解决方案适用于我的情况。即使我尝试为绑定对象设置默认值,它也会在应用程序启动时自动弹出我不需要的微调器,所以请给我建议。

Gab*_*tti 9

可以AutoCompleteTextViewAdapter.

仅举个例子:

<com.google.android.material.textfield.TextInputLayout
        android:id="@+id/til"
        ..>
               
    <com.google.android.material.textfield.MaterialAutoCompleteTextView
       .../>


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

创建适配器:

    ArrayList<String> items = new ArrayList<>();
    items.add("Material");
    items.add("Design");
    items.add("Components");

    ArrayAdapter<String> adapter = new ArrayAdapter<>(this,R.layout.item, items);
    TextInputLayout textInputLayout = findViewById(R.id.til);
Run Code Online (Sandbox Code Playgroud)

设置默认值和适配器:

    ((MaterialAutoCompleteTextView) textInputLayout.getEditText()).setAdapter(adapter);
    ((MaterialAutoCompleteTextView) textInputLayout.getEditText()).setText(adapter.getItem(1),false);
Run Code Online (Sandbox Code Playgroud)

false将过滤器设置为setText(...,false)在下拉列表中显示整个列表而不仅仅是单个值非常重要。

在此输入图像描述