带有AutoCompleteTextView的TextInputLayout的默认文本

4

我正在使用这个来显示TextInputLayoutSpinner类型视图,但是我不知道如何设置任何默认值。

<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>

占位符仅在焦点模式下起作用,请提供一个解决方法。

编辑:

我在这个文本视图中使用双向数据绑定,因此似乎在我的情况下没有解决方案可行。即使我为绑定的对象设置默认值,它也会在应用程序启动时自动弹出旋转器,而我不需要,所以请给我建议。

2个回答

9
AutoCompleteTextView可以与Adapter一起使用。
例如:
<com.google.android.material.textfield.TextInputLayout
        android:id="@+id/til"
        ..>
               
    <com.google.android.material.textfield.MaterialAutoCompleteTextView
       .../>


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

创建适配器:
    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);

设置默认值和适配器:

    ((MaterialAutoCompleteTextView) textInputLayout.getEditText()).setAdapter(adapter);
    ((MaterialAutoCompleteTextView) textInputLayout.getEditText()).setText(adapter.getItem(1),false);

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

似乎您正在为TextInputLayout设置适配器,而不是AutoCompleteTextView... - Cosmic Dev
@CosmicDev 也请查看官方文档:https://github.com/material-components/material-components-android/blob/master/docs/components/TextField.md#implementing-an-exposed-dropdown-menu - Gabriele Mariotti
@CosmicDev不起作用 = 默认文本不可见? - Gabriele Mariotti
是的,它不可见。 - Cosmic Dev
@CosmicDev,这是因为MaterialAutoCompleteTextView由材料组件库自动填充。重要的是setTextsetAdapter的顺序。否则,请使用setText(....,false);。请查看更新后的答案。 - Gabriele Mariotti

3

可以像这样在 AutoCompleteTextView 上使用 android:text 属性

<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:text="Select Something"
                        android:backgroundTint="@android:color/transparent"
                        android:inputType="none"
                        android:lineSpacingExtra="5sp"
                        android:textColor="@color/name_primary_color"
                        android:textSize="14sp" />

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

1
TextInputLayout中加入 android:hint="文本" 也可以达到同样的效果。 - Lalit Fauzdar
1
请翻译以下关于编程的内容,从英文到中文。仅返回翻译后的文本,不作为默认文本。 - Cosmic Dev
1
@AgentP 如果我正在使用数据绑定,会怎样呢? - Cosmic Dev

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接