setTextColor不适用于LiveData和DataBinding

问题描述 投票:0回答:1

我正在尝试使用LiveData绑定textColor和一个视图。为了使用LiveData修改视图的颜色。

我具有以下布局:

<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<data>
    <variable name="fragment" type="com.package.RegisterFragment" />
    <variable name="viewModel" type="com.package.RegistrationViewModel" />
</data>
[......]

                <androidx.appcompat.widget.AppCompatEditText
                    android:id="@+id/password_field"
                    android:background="@drawable/rounded_edit_text"
                    android:layout_width="match_parent"
                    android:layout_height="45dp"
                    android:paddingTop="10dp"
                    android:paddingBottom="10dp"
                    android:text="@={viewModel.password}"
                    android:textColor="@{context.getResources().getColor(viewModel.passwordColor)}"
                    android:paddingRight="10dp"
                    android:paddingLeft="20dp"
                    android:inputType="textPassword"/>

以及下面的代码片段:

private var defaultTextColor: Int = android.R.color.black

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                          savedInstanceState: Bundle?): View? {
    val fragmentBinding: FragmentRegisterBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_register, container, false)
    fragmentBinding.fragment = this
    fragmentBinding.viewModel
    fragmentBinding.lifecycleOwner = this
    convertView = fragmentBinding.root

    viewModel = activity?.run {
        ViewModelProviders.of(this)[RegistrationViewModel::class.java]
    } ?: throw Exception("Invalid Activity")

    (viewModel as RegistrationViewModel).emailColor.value = defaultTextColor

但是我的文字颜色是纯白色。有人知道为什么吗?

android android-databinding android-livedata
1个回答
0
投票
在XML中,我看到您的密码为passColorColor,因此您应该更改passwordColor的值:
© www.soinside.com 2019 - 2024. All rights reserved.