android - 如何从 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"/>
编辑:
问题已解决。我没有在我的片段中设置视图模型。
解决方案
您可以setText
在您的 XML 中使用从项目模型中获取的数据;
<data>
<variable
name="item"
type="com.xxx.xxx" />
</data>
android:text="@{item.xxx}"
推荐阅读
- google-chrome-devtools - 有没有办法阻止黑盒脚本在 chrome 开发工具的 Sources 选项卡中打开?
- node.js - 无法从 error.message 中提取错误消息
- yii2 - 如何从 Kartik 向多个 Select2 添加新值而不是替换旧值?
- python - 从 Python 中的字符串中提取浮点值(可能使用正则表达式)
- python - 创建 azure 函数时缺少 python 语言
- java - 如何修改服务器中运行的java jar文件的代码
- javascript - 如果特定测试用例失败,我希望 Mocha 中止测试执行
- ios - 从 iCloud 访问文件时 Xcode 模拟器错误——“无法与辅助函数通信”iOS 13.1
- if-statement - 检查指定范围内的任何单元格是否满足2个条件
- jsf - ui:param 值在包含的网站中失去其价值(由 ui:include 包含)