在EditText的xml属性里面添加了android:inputType="number|numberDecimal" 小数或者小数点
//数量监听
viewHolder.quantity.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
String editStr = editable.toString().trim();
int posDot = editStr.indexOf(".");
//不允许输入3位小数,超过三位就删掉
if (posDot < 0) {
return;
}
if (editStr.length() - posDot - 1 > 2) {
editable.delete(posDot + 3, posDot + 4);
} else {
//TODO...在这里写逻辑
}
}
});
EditText 只能输入小数点后两位
最新推荐文章于 2021-05-27 20:55:45 发布