DataBinding:根据条件选择字符串资源之一

时间:2017-05-09 14:45:59

标签: android android-databinding

我的数据对象中有一个布尔变量,想要在资源为真时显示1个字符串,在错误时显示另一个字符串。 我试着这样做:

android:text="@{sendit.bccMode ? @string/sharebox.bcc_mode_on : @string/sharebox.bcc_mode_off}"

但是收到编译错误:

  

**** /数据绑定错误**** msg:找不到访问者java.lang.String.bcc_mode_on

我做错了什么?

2 个答案:

答案 0 :(得分:3)

使用时,Databinding库会丢失。 (点)作为名称,将您的strings.xml文件更改为:

  <string name="sharebox_bcc_mode_on">BCC mode on</string>
  <string name="sharebox_bcc_mode_off">BCC mode off</string>

答案 1 :(得分:0)

确切地说,您不必使用下划线代替点来重命名字符串,而在使用此字符串时只需用下划线替换点即可:

在strings.xml文件中:

<string name="sharebox.bcc.mode.on">BCC mode on</string>
<string name="sharebox.bcc.mode.off">BCC mode off</string>

在数据绑定中(在activity.xml中):

android:text="@{sendit.bccMode ? @string/sharebox_bcc_mode_on : @string/sharebox_bcc_mode_off}"

所以您只需要转换'。'。在数据绑定中使用此字符串时,将其设置为“ _”

相关问题