dzi*_*kyy 5 data-binding android android-databinding
如何使用数据绑定将我的对象String字段值与xml文件中的另一个String值进行比较?是否可以在xml文件中执行此操作,还是应该使用@BindingAdapter注释在项目中的某处创建方法?以下是我到目前为止所尝试的内容并没有奏效.与String资源值进行比较而不是与硬编码字符串值进行比较也是很好的.
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/male"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="@{user.gender.equalsIgnoreCase("male")}"
android:text="@string/male"/>
<RadioButton
android:id="@+id/female"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="@{user.gender.equalsIgnoreCase("female")}"
android:text="@string/female"/>
</RadioGroup>
Run Code Online (Sandbox Code Playgroud)
感谢帮助.
Geo*_*unt 16
你几乎是正确的.字符串常量不能在XML中使用双引号内的双引号,因此android数据绑定支持在表达式中使用反引号:
<RadioButton
android:id="@+id/male"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="@{user.gender.equalsIgnoreCase(`male`)}"
android:text="@string/male"/>
Run Code Online (Sandbox Code Playgroud)
这允许您将字符常量与单引号以及字符串常量混合使用.
XML还允许对属性值使用单引号,因此您可以在表达式中使用双引号.这是更常见的方法:
<RadioButton
android:id="@+id/female"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked='@{user.gender.equalsIgnoreCase("female")}'
android:text="@string/female"/>
Run Code Online (Sandbox Code Playgroud)
您可以跳过整个事情并使用字符串资源或常量:
<RadioButton
android:id="@+id/male"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="@{user.gender.equalsIgnoreCase(@string/male)}"
android:text="@string/male"/>
<RadioButton
android:id="@+id/female"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="@{user.gender.equalsIgnoreCase(StringConstants.FEMALE)}"
android:text="@string/female"/>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5330 次 |
| 最近记录: |