在EditText中的金额字段中智能取消屏蔽逗号

时间:2014-12-17 10:39:36

标签: android android-edittext

我有一个数量的编辑文本,其输入类型是数字。

当我在金额字段中输入数字时,我使用Text Watcher中的AfterTextChanged方法进行解析以及应用货币掩码' ###,###,###'在键入的数字中插入逗号,以便显示的金额字段易于阅读。

DecimalFormat formatter = new DecimalFormat("###,###,###");
String formattedString = formatter.format(number);
editText.setText(formattedString);

但是,在为editText输入值14580000时,我收到此错误

java.lang.NumberFormatException: Invalid int: "14,580,000"
             at java.lang.Integer.invalidInt(Integer.java:138)
             at java.lang.Integer.parse(Integer.java:410)
             at java.lang.Integer.parseInt(Integer.java:367)
             at java.lang.Integer.parseInt(Integer.java:334)

该错误是解析(Integer.parseInt)具有逗号的编辑文本字段的结果。因此,有一种智能方式,Android可以将编辑文本值识别为屏蔽值,从而自动取消屏蔽iestrip off the逗号。

或者,这是我甚至不应该尝试使用"数字"编辑文本。输入类型?

1 个答案:

答案 0 :(得分:0)

将String解析为整数时使用

String.replaceAll(String, String) 
像这样:

Integer.parseInt(formattedString.replaceAll(",",""));

这将删除(替换为空字符串)所有逗号,并且应该成功解析字符串。