首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >一个字符串中的两个或多个plular在android中

一个字符串中的两个或多个plular在android中
EN

Stack Overflow用户
提问于 2017-02-20 09:24:19
回答 2查看 1.5K关注 0票数 1

我必须在android中插入一个xml字符串,两个复数,以满足用户的年龄。我得处理sutch的案子

  1. “你三个月大”(适用于使用儿童档案的父母)
  2. “你一岁了”
  3. “你一岁一个月大”
  4. “你已经一岁零三个月了”
  5. “你两岁了”
  6. “你3岁1个月大”
  7. “你5岁3个月大”

正如你只看到的英语,我需要6个不同的情况。另一种语言就更多了。另外,我也不想把字符串拆分成两个复数,因为在同一种语言中,世界的顺序是不同的。

到目前为止,我只用复数表示一个数量。在文档中,我没有看到任何解决sutch问题的提示?

实际解决办法:

在xml中:

代码语言:javascript
复制
<string name="age_1">You are %1$s old</string>
<string name="age_2">You are %1$s and %2$s old</string>

<plurals name="age_months">
    <item quantity="one">%d month</item>
    <item quantity="other">%d months</item>
</plurals>

<plurals name="age_years">
    <item quantity="one">%d year</item>
    <item quantity="other">%d years</item>
</plurals>

在代码中:

代码语言:javascript
复制
   if(years == 0) {
      String m = res.getQuantityString(R.plurals.age_months, months, months);
      ageTextView.setText(String.format(res.getString(R.age_1), m);
   } else if (months == 0){
      String y = res.getQuantityString(R.plurals.age_years, years, years);
      ageTextView.setText(String.format(res.getString(R.age_1), y);
    } else {
      String m = res.getQuantityString(R.plurals.age_months, months, months);
      String y = res.getQuantityString(R.plurals.age_years, years, years);
      ageTextView.setText(String.format(res.getString(R.age_2), y, m);

    }

但是我正在寻找在xml中使用一个复数内容的东西:

代码语言:javascript
复制
   <plurals name="age_months">
        <item quantityA="one" quantityB="one">You are %1$d year %2$d month</item>
        <item quantityA="other"quantityB="one">You are %1$d years %2d month</item>
...
    </plurals>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-02-20 13:50:16

因此,似乎没有比我所用的更好的解决办法:

在xml中:

代码语言:javascript
复制
<string name="age_1">You are %1$s old</string>
<string name="age_2">You are %1$s and %2$s old</string>

<plurals name="age_months">
    <item quantity="one">%d month</item>
    <item quantity="other">%d months</item>
</plurals>

<plurals name="age_years">
    <item quantity="one">%d year</item>
    <item quantity="other">%d years</item>
</plurals>

以及代码中的一些逻辑:

代码语言:javascript
复制
   if(years == 0) {
      String m = res.getQuantityString(R.plurals.age_months, months, months);
      ageTextView.setText(String.format(res.getString(R.age_1), m);
   } else if (months == 0){
      String y = res.getQuantityString(R.plurals.age_years, years, years);
      ageTextView.setText(String.format(res.getString(R.age_1), y);
    } else {
      String m = res.getQuantityString(R.plurals.age_months, months, months);
      String y = res.getQuantityString(R.plurals.age_years, years, years);
      ageTextView.setText(String.format(res.getString(R.age_2), y, m);

    }
票数 0
EN

Stack Overflow用户

发布于 2017-02-20 09:29:28

看看这个。

数量字符串(Plurals)

https://developer.android.com/guide/topics/resources/string-resource.html#Plurals

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <plurals
        name="plural_name">
        <item
            quantity=["zero" | "one" | "two" | "few" | "many" | "other"]
            >text_string</item>
    </plurals>
</resources>
票数 -2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42340687

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档