【发布时间】:2019-07-09 13:18:42
【问题描述】:
我想使用 TextWatcher 将 3 个小数位 货币格式 添加到 EditText 一开始,值是0.000,数字应该从右到左变化
例如:如果我按顺序按 1、2、3、4、5,则值应显示为 12.345
以下代码仅适用于小数点后 2 位。请任何人帮助我如何将此代码更改为小数点后 3 位或其他解决方案
public class CurrencyTextWatcher implements TextWatcher {
boolean mEditing;
Context context;
public CurrencyTextWatcher() {
mEditing = false;
}
public synchronized void afterTextChanged(Editable s) {
if(!mEditing) {
mEditing = true;
String digits = s.toString().replaceAll("\\D", "");
NumberFormat nf = NumberFormat.getCurrencyInstance();
try{
String formatted = nf.format(Double.parseDouble(digits)/100);
s.replace(0, s.length(), formatted);
} catch (NumberFormatException nfe) {
s.clear();
}
mEditing = false;
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
public void onTextChanged(CharSequence s, int start, int before, int count) { }
}
【问题讨论】:
标签: java android android-edittext number-formatting android-textwatcher