2017-02-20 30 views
0

我必須在android中爲用戶的年齡放置兩個xml字符串。我必須處理sutch情況:android xml中的一個字符串中的兩個或更多的羽毛

  1. 「你是3個月」(父母其中使用童工的個人資料)
  2. 「你是1歲」
  3. 「你1年和1月老「
  4. ‘你是1年零3個月大的’
  5. ‘你是2歲的’
  6. ‘你是3歲零1個月大的’
  7. 」你是5年3個月「

而正如你所看到的僅用於英語,我需要6個不同的案例。對於另一種語言更多。而且我不想在句子中以相同語言的順序打斷兩個複數形式的詞串不一樣。

直到現在我alwas使用複數只有一個數量。在文檔中,我沒有看到任何提示如何糾正解決sutch問題?

實際的解決方案:

在XML

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

而在代碼:

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使用一個複數含量的不同的東西:

<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> 
+0

你只需要'year','years'和'month','months'。兩個單數和他們相應的複數。那麼,有什麼問題呢? –

+0

@Rotwang在其他語言的順序可以是不同的,如「年齡是3個monts和2年」,我不想分裂一個句子。 – LunaVulpo

+0

它有什麼變化?你總是必須拆分句子。如果你想讓事情正確完成。 –

回答

0

如此看來,有比我以前沒有bether解決方案:

在XML:

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

和代碼中的一些邏輯:

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); 

    } 
相關問題