android - 使用数据绑定(bind)时 TextInputLayout 提示不起作用

标签 android data-binding viewmodel android-textinputedittext

我将数据绑定(bind)与 TextInputLayout/TextInputEditText 组合一起使用,如 xml 中所示。

<com.google.android.material.textfield.TextInputLayout
                android:id="@+id/lastname_input_layout"
                style="@style/TextInputLayout"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                app:endIconMode="clear_text"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/firstname_input_layout"
                app:visible="@{viewModel.showFields}">

                <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/lastname_input"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="@string/lastname"
                    android:inputType="textCapWords"
                    android:text="@={viewModel.lastName}"
                    android:textAppearance="@style/TextAppearance.Input" />

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

            <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/email_input_layout"
                style="@style/TextInputLayout"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                app:endIconMode="clear_text"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/lastname_input_layout"
                app:visible="@{viewModel.showFields}">

                <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/email_input"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="@{viewModel.watchMediaTextHint}"
                    android:inputType="@{viewModel.watchMediaInputType}"
                    android:text="@={viewModel.mediaText}"
                    android:textAppearance="@style/TextAppearance.Input" />

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





如您所见,这两个字段在理论上是相同的,唯一的区别是其中一个字段具有 hint用字符串资源给出的文本,另一个用数据绑定(bind)给出。两者具有相同的样式,文本外观等,但是,TextInputEditText 上的提示使用数据绑定(bind)有不同的颜色。此外,当 View 获得焦点时,标签不会像第一个字段那样动画。

使用字符串资源设置提示可以使这种行为恢复正常,关于为什么会发生这种异常的任何想法?我宁愿通过数据绑定(bind)而不是编程解决方案来解决这个问题。

谢谢。

最佳答案

hint如果属性设置为 <TextInputLayout>,则该属性仅与数据绑定(bind)结合使用。而不是 <TextInputEditText> .<TextInputEditText>小号 hint属性仅在膨胀期间应用一次,因此在与数据绑定(bind)结合使用时不会更新/应用。

请注意@Mike M. 的评论。我将其转换为答案,以便其他人更容易找到。

关于android - 使用数据绑定(bind)时 TextInputLayout 提示不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61466930/

相关文章:

android - setDisplayHomeAsUpEnabled() + 向上箭头?

java - 错误:调用需要API级别23(当前最小值为15):

c# - 如何在 WPF XAML 中使用嵌套类?

wpf - 我的静态模型/属性未绑定(bind)到我的 UI

android - 如何在 MVVM/retrofit Architecture 中使用 DataBinding 在 RecyclerView 的一行中设置数据?

asp.net-mvc - MVC ASP.NET 没有为此对象定义无参数构造函数

c# - 从列表框中获取选中的项目

android - 如何填充 Java 项目中每个函数的 log/println 调用?

android - 在 Android 中存储用户登录凭据的最佳位置

android - Jetpack Compose MutableLiveData 不更新 UI 组件