简体   繁体   中英

setTextColor doesn't work with LiveData and DataBinding

I'm trying to bind using LiveData the textColor and a view. In order to modify the view's color with the LiveData.

I've the following layout:

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

And the following code in my fragment:

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

But my text color is a solid white. Anyone knows why?

In XML, I see your binding with pass passwordColor, so you should change value of passwordColor:

(viewModel as RegistrationViewModel).passwordColor.value = defaultTextColor

Params in xml and livedata are same.

I have to change my XML like this:

                <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="@{viewModel.passwordColor}"
                    android:paddingRight="10dp"
                    android:paddingLeft="20dp"
                    android:inputType="textPassword"/>

And in my code I changed the color to:

private var defaultTextColor: Int = Color.BLACK

The missing viewModel actually doesn't effect the functioning of the app (really strange though).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM