我的应用在spinner中有一个语言更改选项,使用的语言如下 1.英语 2.意大利 3.简体中文 4.繁体中文
我已在相应的值文件夹中加载了所有语言字符串。但是,当我试图加载繁体中文时,我只能获得简体中文文本。
在google中搜索此内容后,我尝试根据Locale加载语言。
以下是我的Spinner选择和操作代码
langSpinner = (Spinner)this.findViewById(R.id.lanuage_spinner1);
langSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int pos, long arg3)
{
if (pos == 0)
{
langSelected ="en";
locale = Locale.ENGLISH;
}
else if (pos == 1)
{
langSelected ="it";
locale = Locale.ITALIAN;
}
else if (pos == 2)
{
langSelected ="zh";
locale = Locale.SIMPLIFIED_CHINESE;
}
else if (pos == 3)
{
langSelected ="zh-rTW";
locale = Locale.TRADITIONAL_CHINESE;
}
changeLang(langSelected, pos);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
public void changeLang(String lang, int pos)
{
if (lang.length() != 0)
{
//locale = new Locale(lang);
android.content.res.Configuration config = new android.content.res.Configuration();
config.locale = locale;
Locale.setDefault(locale);
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
// here using intent call the same activity again
}
}
现在我的问题出在我用来显示货币价值的下一个活动&#34; $&#34;,从我的后端服务器我用来获取货币代码为USD并通过以下代码解析我使用显示&#34; $&#34;
public static Currency parseCurrencyCode(String currencyCode) {
if (currencyCode.equals("RMB")) {
return Currency.getInstance("CNY");
}
try {
return Currency.getInstance(currencyCode);
} catch (IllegalArgumentException e) {
Log.e(TAG, "Unknown Currency " + currencyCode);
}
}
添加上述用于加载繁体中文字符串的Locale后,我没有得到&#34; $&#34;值。
If selected language is English, currency will be as USD
If selected language is Italy, currency will be as USD
If selected language is Simplified Chinese, currency will be as US$
If selected language is Traditional Chinese, currency will be as $
为什么会这样?
我已经通过this,说由于语言环境,某些货币用于显示,如上所述。如何克服这个问题。
答案 0 :(得分:0)
通过使用以下方法,我可以获得货币代码
String currencyCode = "USD";
Currency currency = Currency.getInstance(currencyCode);
NumberFormat format = NumberFormat.getCurrencyInstance();
format.setMaximumFractionDigits(digits);
String symbol = currency.getSymbol(Locale.US);
DecimalFormatSymbols decimalFormatSymbols = ((java.text.DecimalFormat) format).getDecimalFormatSymbols();
decimalFormatSymbols.setCurrencySymbol(symbol);
((java.text.DecimalFormat) format).setDecimalFormatSymbols(decimalFormatSymbols);
return format.format(value);