首页 > 解决方案 > 如何从 Fragment/Activity 在 binder TextView 中设置文本?

问题描述

我在我的 XML 中有通过 DataBinding 双向绑定的 TextView,我想从 Fragment 设置文本,然后将其传递给 ViewModel。这个有可能?

<TextView
        android:id="@+id/location_text_view"
        android:text="@={hourlyViewModel.location}"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:textColor="@color/colorWhite"
        android:elevation="2dp"
        android:textSize="18sp"
        android:gravity="center"/>

编辑:

问题已解决。我没有在我的片段中设置视图模型。

标签: androidandroid-databinding

解决方案


您可以setText在您的 XML 中使用从项目模型中获取的数据;

<data>
<variable
     name="item"
     type="com.xxx.xxx" />
</data>

android:text="@{item.xxx}"


推荐阅读