您的位置:首页 > 移动开发 > Android开发

android给EditText加金额的属性,带元,角和分,如¥12.34

2016-03-18 13:51 363 查看
功能如标题,代码如下:  (本代码
可直接用到你的项目中,
只需要将EditText transactionMoney替换成你的即可)
String abc = "";
EditText transactionMoney;//就是随便一个输入框,表示金额
//下面就是给editText加一个文字监听

transactionMoney.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}

@Override
public void afterTextChanged(Editable s) {
if (!s.toString().equals(abc)) {
transactionMoney.removeTextChangedListener(this);
String replaceable = String.format("[%s, \\s.]", NumberFormat.getCurrencyInstance(Locale.CHINA).getCurrency().getSymbol(Locale.CHINA));
String cleanString = s.toString().replaceAll(replaceable, "");

if (cleanString.equals("") || new BigDecimal(cleanString).toString().equals("0")) {
transactionMoney.setText(null);

4000
} else {
double parsed = Double.parseDouble(cleanString);
String formatted = NumberFormat.getCurrencyInstance(Locale.CHINA).format((parsed / 100));
abc = formatted;
transactionMoney.setText(formatted);
transactionMoney.setSelection(formatted.length());
}
transactionMoney.addTextChangedListener(this);
}
}
});

希望对您有所帮助!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息