复数在 xml 中不起作用

Шах*_*Шах 4 android android-databinding

我试图在我的错误文本中加载复数TextInputEditText

<android.support.design.widget.TextInputEditText
    android:id="@+id/et_subject_name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="textCapWords"
    android:lines="1"
    android:maxLines="1"
    android:singleLine="true"
    android:text="@{ model.name }"
    android:hint="@string/hint_name"
    app:validateRegex='@{"^*\\S{2}.*$"}'
    app:validateRegexMessage="@{@plurals/error_too_short(model.minNameLength)}"/>
Run Code Online (Sandbox Code Playgroud)

但是在我的应用程序中,它显示了我的字符串而%d不是model.minNameLength值。这app:validateRegex是一个由数据绑定验证器库定义的属性。
我的复数:

<plurals name="error_too_short">
    <item quantity="one" >Too short, enter at least %d visible character.</item>
    <item quantity="other" >Too short, enter at least %d visible characters.</item>
</plurals>
Run Code Online (Sandbox Code Playgroud)

怎么了?或者有另一种在xml中显示复数的方法吗?
PS 如果重要的话,我使用数据绑定引擎 V2。

Sar*_*lla 17

在官方Android 文档中

使用getQuantityString()方法时,如果您的字符串包含带有数字的字符串格式,则您需要将计数传递两次。例如,对于找到的字符串 %d 歌曲,第一个 count 参数选择适当的复数字符串,第二个 count 参数插入到 %d 占位符中。如果您的复数字符串不包含字符串格式,则不需要将第三个参数传递给getQuantityString

因此,当您要model.minNameLength定义要选择的复数版本以及将其值插入字符串时,您应该提供两次。所以,数据绑定表达式应该是这样的:

app:validateRegexMessage="@{@plurals/error_too_short(model.minNameLength, model.minNameLength)}"
Run Code Online (Sandbox Code Playgroud)


Ell*_*lar 1

它应该看起来像这样:

<plurals name="recent_messages">
    <item quantity="zero">No Recent Messages</item>
    <item quantity="one">1 Recent Message</item>
    <item quantity="other">%1$d Recent Messages</item>
</plurals>

mContext.getResources().getQuantityString(R.plurals.recent_messages, count, count);
Run Code Online (Sandbox Code Playgroud)

  • 您可以使用“%d”。 (3认同)