android 阿拉伯数字转汉字,Android中阿拉伯文字的阿拉伯数字

谷歌的bugtracker中存在这样的问题:

Arabic numerals in arabic language intead of Hindu-Arabic numeral system

如果由于某些客户的问题(我可以理解)特别是埃及语言环境不起作用,那么您可以将字符串格式化为任何其他西方语言环境.例如:

NumberFormat nf = NumberFormat.getInstance(new Locale("en","US")); //or "nb","No" - for Norway

String sDistance = nf.format(distance);

distanceTextView.setText(String.format(getString(R.string.distance), sDistance));

如果具有新Locale的解决方案根本不起作用,那么有一个丑陋的解决方法:

public String replaceArabicNumbers(String original) {

return original.replaceAll("١","1")

.replaceAll("٢","2")

.replaceAll("٣","3")

.....;

}

(以及与Unicodes匹配的变体(U 0661,U 0662,…).查看更多类似的想法here)

Upd1:

为了避免在任何地方逐个调用格式化字符串,我建议创建一个小工具方法:

public final class Tools {

static NumberFormat numberFormat = NumberFormat.getInstance(new Locale("en","US"));

public static String getString(Resources resources, int stringId, Object... formatArgs) {

if (formatArgs == null || formatArgs.length == 0) {

return resources.getString(stringId, formatArgs);

}

Object[] formattedArgs = new Object[formatArgs.length];

for (int i = 0; i < formatArgs.length; i++) {

formattedArgs[i] = (formatArgs[i] instanceof Number) ?

numberFormat.format(formatArgs[i]) :

formatArgs[i];

}

return resources.getString(stringId, formattedArgs);

}

}

....

distanceText.setText(Tools.getString(getResources(), R.string.distance, 24));

或者覆盖默认的TextView并在setText中处理它(CharSequence文本,BufferType类型)

public class TextViewWithArabicDigits extends TextView {

public TextViewWithArabicDigits(Context context) {

super(context);

}

public TextViewWithArabicDigits(Context context, AttributeSet attrs) {

super(context, attrs);

}

@Override

public void setText(CharSequence text, BufferType type) {

super.setText(replaceArabicNumbers(text), type);

}

private String replaceArabicNumbers(CharSequence original) {

if (original != null) {

return original.toString().replaceAll("١","1")

.replaceAll("٢","2")

.replaceAll("٣","3")

....;

}

return null;

}

}

我希望,这有帮助

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值