如何使用数据绑定比较 xml 文件中的字符串
How to compare strings in xml file using databinding
如何使用数据绑定将我的对象字符串字段值与 xml 文件中的另一个字符串值进行比较?是否可以在 xml 文件中这样做,或者我应该在项目中的某处使用 @BindingAdapter
注释创建一个方法?
以下是我到目前为止尝试过的方法,但没有奏效。与字符串资源值而不是硬编码字符串值进行比较也很好。
<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>
感谢您的帮助。
你几乎是正确的。字符串常量不能在 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"/>
这允许您将字符常量与单引号和字符串常量混合使用。
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"/>
您可以跳过整个过程并使用字符串资源或常量:
<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"/>
如何使用数据绑定将我的对象字符串字段值与 xml 文件中的另一个字符串值进行比较?是否可以在 xml 文件中这样做,或者我应该在项目中的某处使用 @BindingAdapter
注释创建一个方法?
以下是我到目前为止尝试过的方法,但没有奏效。与字符串资源值而不是硬编码字符串值进行比较也很好。
<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>
感谢您的帮助。
你几乎是正确的。字符串常量不能在 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"/>
这允许您将字符常量与单引号和字符串常量混合使用。
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"/>
您可以跳过整个过程并使用字符串资源或常量:
<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"/>