【问题标题】:setTextColor doesn't work with LiveData and DataBindingsetTextColor 不适用于 LiveData 和 DataBinding
【发布时间】:2020-01-15 18:03:12
【问题描述】:

我正在尝试使用 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

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

【问题讨论】:

  • fragmentBinding.viewModel 尚未设置为任何值。应该是fragmentBinding.viewModel = viewModel,在viewModel初始化之后写。

标签: android android-databinding android-livedata


【解决方案1】:

在 XML 中,我看到您与 pass passwordColor 绑定,所以您应该更改 passwordColor 的值:

(viewModel as RegistrationViewModel).passwordColor.value = defaultTextColor

xml 和 livedata 中的参数相同。

【讨论】:

    【解决方案2】:

    我必须像这样更改我的 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 实际上不会影响应用程序的功能(虽然真的很奇怪)。

    【讨论】:

      猜你喜欢
      • 2019-12-10
      • 2019-02-16
      • 2021-03-01
      • 2016-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多