Android 带有AutoCompleteTextView的TextInputLayout的默认文本
我用它来显示Android 带有AutoCompleteTextView的TextInputLayout的默认文本,android,autocompletetextview,material-components-android,android-textinputlayout,Android,Autocompletetextview,Material Components Android,Android Textinputlayout,我用它来显示TextInputLayout的Spinner视图类型,但我不知道如何设置它的默认值 <com.google.android.material.textfield.TextInputLayout style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox.ExposedDropdownMenu" android:layout_width="match_paren
TextInputLayout
的Spinner
视图类型,但我不知道如何设置它的默认值
<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>
占位符仅在焦点模式下工作,因此请建议解决方法
编辑:
我在这个文本视图中使用双向数据绑定,因此在我的案例中似乎没有解决方案。即使我尝试为绑定对象设置默认值,它也会在应用程序启动时自动弹出微调器,我不需要它,所以请给我一些建议。像这样使用
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>
自动完成文本视图可以与适配器一起工作
举个例子:
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/til"
..>
<com.google.android.material.textfield.MaterialAutoCompleteTextView
.../>
</com.google.android.material.textfield.TextInputLayout>
将setText(…,false)
中的过滤器false
设置为
在下拉列表中显示整个列表,而不仅仅是单个值
putandroid:hint=“text”TextInputLayout
中的
也起到了作用。这不会作为默认文本。@AgentP如果我使用数据绑定怎么办?似乎您正在为TextInputLayout设置适配器,而不是在自动完成textview上…@CosmicDev检查官方文档:@CosmicDev未工作=默认文本不可见?是的visible@CosmicDev这是相同的,因为有MaterialAutoCompleteTextView
是由材质组件库自动膨胀的。重要的是setxt
和setAdapter
的顺序。否则使用setText(..,false)代码>。检查更新的答案。
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);