首页 > 解决方案 > 绑定 Html 的用户数据绑定。fromHtml 字符串 android

问题描述

嗨,我是 mvvm 结构的新手,我在这里学习新东西。目前,我已将默认字符串与我的视图绑定,我有多种颜色的文本,因此我选择使用 HTML.fomHTml 螺母,我无法使用,任何人都可以帮助解决我的测试代码下方的此绑定问题....

资料>

    <import type="android.text.Html"/>

    <variable
        name="loginviewmodel"
        type="app.road2xtech.neighbourhood.view.viewmodel.LoginViewModel" />

</data>



  <TextView
        android:id="@+id/textViewAccount"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="56dp"
        android:fontFamily="@font/raleway_regular"
        android:gravity="center"
        android:text="@={Html.fromHtml(loginviewmodel.accountString)}"
        android:textColor="@android:color/black"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

它会给我以下错误

The expression 'android.text.Html.fromHtml(loginviewmodelAccountString)' cannot be inverted, so it cannot be used in a two-way binding

Details: There is no inverse for method fromHtml, you must add an @InverseMethod annotation to the method to indicate which method should be used when using it in two-way binding expressions

标签: androidandroid-viewmodelandroid-binder

解决方案


这称为双向数据绑定

android:text="@={Html.fromHtml(loginviewmodel.accountString)}"

InverseMethod注释可以应用于双向数据绑定中使用的任何方法。因此,如果您不需要双向绑定,那么总结一下您的 XML,然后请使用它。

 android:text="@{Html.fromHtml(loginviewmodel.accountString)}"

这将起作用。:)


推荐阅读