txtAmount = (EditText) findViewById(R.id.amount);
txtAmount.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
String text = s.toString();
if (text.contains(".")) {
int index = text.indexOf(".");
if (index + 3 < text.length()) {
text = text.substring(0, index + 3);
txtAmount.setText(text);
txtAmount.setSelection(text.length());
}
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
android EditText限制只能输入2位小数的解决方法
最新推荐文章于 2024-07-08 03:08:18 发布
本文介绍了一种在Android应用中使用EditText控件时限制用户输入的小数位数不超过三位的方法。通过添加TextWatcher监听器并在用户输入时实时检查字符串内容,确保超出部分不会被保存。
1495

被折叠的 条评论
为什么被折叠?



