我想在 android、数据绑定 Api 中为 TextView 连接两个字符串

I want to concat two strings for a TextView in android, Data Binding Api

我正在使用 DataBinding Api 在 android 布局中设置视图。这是我的布局。

layout.xml

<?xml version="1.0" encoding="utf-8"?>
 <layout xmlns:android="http://schemas.android.com/apk/res/android">
  <data>
    <variable name="user" type="testing.sampleapp.com.sampleapp.User"/>
  </data>
<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@{ "Hello " + user.firstName}"/>
</LinearLayout>

我希望 TextView 显示 Hello UserName。如何使用数据绑定 api.

实现此目的

重音符 (`)

连接
android:text="@{`Hello ` + user.firstName}"/>

您可以通过多种方式连接它,请在此处查看concat-two-strings-in-textview-using-databinding

有两种方法。

第一个解决方案

用重音符号连接 (`)

android:text="@{`Hello ` + user.firstName}"/>

第二种解法

strings.xml

中声明您的字符串

喜欢"Hello %1$s , (whatever you want to add then add here)".

amd 使用 String.format(stringResource, upsatename);

由于 xml 支持属性值的单引号,您也可以这样做:

android:text='@{"Hello "+user.firstName}'

要在 xml 布局中进行连接:

<data>

/*This is used for android view*/
<import type="android.view.View" />

/*This is used for android resources*/
<import type="com.myapp.R" />

/*This is app context*/
<variable
    name="context"
    type="android.content.Context" />

/*This is used for model data*/
<variable
    name="item"
    type="com.myapp.models.Chapter" />
</data>

android:text="@{item.serialNo.concat(@string/space).concat(item.title)}"

在 strings.xml 中我添加了空白代码 space:

<string name="space">\u0020</string>

@GeorgeMount 在对其中一个解决方案的评论中已经回答了这个问题。在我看来,这是迄今为止最好的解决方案。

android:text="@{@string/location(user.city,user.state)}"

在你的strings.xml

<string name="location">%1$s, %2$s</string>

如果您想将 String 资源与模型中的数据连接起来,您可以通过以下方式进行:

 android:text='@{@string/release_date+model.release_date}'

连接字符串的多种方式

1。使用字符串资源(最优选,因为本地化

android:text= "@{@string/generic_name(user.name)}"

像这样制作字符串资源。

<string name="generic_name">Hello %s</string>

2。硬编码连接

android:text="@{`Hello ` + user.name}"/>

3。使用 String 的连接方法

android:text="@{user.firstName.concat(@string/space).concat(user.lastName)}"

这里 space 是一个 html 实体,它被放置在 strings.xml 中。因为 XML 不直接接受 Html 实体或特殊字符。

<string name="space">\u0020</string>

4。使用 String.format()

android:text= "@{String.format(@string/hello, user.name)}"

你必须在这种类型的布局中导入字符串 class。

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <data>
        <import type="String" />
    </data>
    <TextView
        android:text= "@{String.format(@string/hello, user.name)}"
        ... >
    </TextView>
</layout>

5。另一种方法

android:text="@{@string/generic_name(user.firstName,user.lastName)}"

在这种情况下,将字符串资源放入 strings.xml

<string name="generic_name">%1$s, %2$s</string>

还有很多方法,选择你需要的一种。

我发现最简单的方法是用 ''(single) 代替 ""(double), 对于例如。你有两个变量,

<variable name="a" type="String" />
<variable name="b" type="String" />

现在连接,

android:text='a + " " + b}'

在静态字符串和其他动态字符串的情况下你可以使用这个

android:text="@{`Hello ` + user.firstName}"/>

如果是动态数据,您可以使用它。

android:text='@{user.firstName+" "+user.lastName}'

聚会可能迟到了:下面的代码也可以工作

android:text='@{@string/hello.concat(user.firstName)}'

我少花钱

如果您想检查 null 并设置默认值试试这个答案

 android:text='@{(user.first_name ?? "") +" "+ (user.last_name ?? "")}'