setTextColor 不适用于 LiveData 和 DataBinding
setTextColor doesn't work with LiveData and DataBinding
我正在尝试使用 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
但是我的文字颜色是纯白色。有人知道为什么吗?
在 XML 中,我看到您的绑定传递了 passwordColor,因此您应该更改 passwordColor 的值:
(viewModel as RegistrationViewModel).passwordColor.value = defaultTextColor
xml和livedata中的参数相同
我必须像这样更改我的 XML:
<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"/>
在我的代码中,我将颜色更改为:
private var defaultTextColor: Int = Color.BLACK
缺少的 viewModel 实际上不会影响应用程序的功能(虽然真的很奇怪)。
我正在尝试使用 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
但是我的文字颜色是纯白色。有人知道为什么吗?
在 XML 中,我看到您的绑定传递了 passwordColor,因此您应该更改 passwordColor 的值:
(viewModel as RegistrationViewModel).passwordColor.value = defaultTextColor
xml和livedata中的参数相同
我必须像这样更改我的 XML:
<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"/>
在我的代码中,我将颜色更改为:
private var defaultTextColor: Int = Color.BLACK
缺少的 viewModel 实际上不会影响应用程序的功能(虽然真的很奇怪)。