通过设置TextWatcher监听,每次SetText之后,会重新调用afterTextChanged , 因此要每次进行判断,反正造成死循环。
EditText.addTextChangedListener(new TextWatcher() {
DecimalFormat df = new DecimalFormat(",###");
String oldValue = "";
@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) {
String money = s.toString().replace(",", "");
if (!oldValue.equals(money)) {
oldValue = money;
if (!StringUtils.isEmpty(money)) {
String formatMoney = df.format(Integer.valueOf(money));
etTelSendMoeny.setText(formatMoney);
if (s.toString().length() != 0) {
etTelSendMoeny.setSelection(formatMoney.length());
}
} else {
etTelSendMoeny.setText("");
}
}
}
});