如何使用数据绑定比较xml文件中的字符串 [英] How to compare strings in xml file using databinding

查看:147
本文介绍了如何使用数据绑定比较xml文件中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用数据绑定将对象的String字段值与xml文件中的另一个String值进行比较?是否可以在xml文件中执行此操作,还是应该在我的项目中的某个位置创建带有@BindingAdapter批注的方法? 以下是我到目前为止尝试过的方法,但没有奏效.与String资源值而不是硬编码字符串值进行比较也将是很好的.

How can I compare my object String field value to another String value in xml file using databinding? Is it possible to do so in xml file or should I create a method somewhere in my project with @BindingAdapter annotation? Below is what I've tried so far and it didn't worked. It would also be good to compare with String resource value and not with hardcoded string value.

<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数据绑定支持在表达式中使用反引号:

You have it almost correct. String constants can't use double-quotes within double-quotes in XML, so android data binding supports using back-quote in the expression:

        <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"/>

这使您可以将字符常量与单引号以及字符串常量混合使用.

That allows you to mix character constants with single-quotes along with string constants.

XML还允许对属性值使用单引号,因此可以在表达式中使用双引号.这是更常见的方法:

XML also allows the use of single quotes for attribute values, so you can use double quotes within the expression. This is the more common approach:

        <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"/>

您可以跳过全部内容并使用字符串资源或常量:

You can skip the whole thing and use string resources or constants:

        <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文件中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆