我在应用程序中有这个计划,当用户在编辑文本中输入新编号时,编辑文本将其转换为货币,例如用户输入:
123456
并向用户显示:
123456
为此目的写下这段代码:
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if(!charSequence.toString().equals(current)){
price.removeTextChangedListener(this);
String cleanString = charSequence.toString().replaceAll("[$,.]", "");
Double parsed = Double.parseDouble(cleanString);
DecimalFormat df1 = new DecimalFormat( "#.##" );
NumberFormat nf=NumberFormat.getCurrencyInstance();
DecimalFormatSymbols decimalFormatSymbols = ((DecimalFormat) nf).getDecimalFormatSymbols();
decimalFormatSymbols.setCurrencySymbol("");
((DecimalFormat) nf).setDecimalFormatSymbols(decimalFormatSymbols);
String res=nf.format((parsed/100)).trim();
current = res;
price.setText(res);
price.setSelection(res.length());
price.addTextChangedListener(this);
}
但该代码首次在编辑文本中显示0.00
,例如输入123456
时向用户显示:1,234.56
如何解决该问题?谢谢