mEdtOilHeightWarning.addTextChangedListener(getWatcher(mEdtOilHeightWarning));android:inputType="numberDecimal"private TextWatcher getWatcher(EditText edt) { return new TextWatcher() { @Override public void beforeTextChanged(CharSequence s,
return 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) {
String text = s.toString();
if (TextUtils.isEmpty(text)) {
return;
}
if (text.startsWith("00")) {
edt.setText(text.replace("00", "0"));//去除00开头
}
if (text.startsWith("0") && text.length() >= 2 && !".".equals(text.charAt(1) + "")) {
edt.setText(text.substring(1));//替换整数0开头 输入01显示1
}
if (text.startsWith(".")) {
edt.setText(String.format("0%s", text));//.开头替换为0.开头
}
if (text.contains(".") && (text.length() - text.indexOf(".")) > 3) {
edt.setText(text.substring(0, text.length() - 1));//保留两位小说
}
if (text.length() > 7) {
edt.setText(text.substring(0, text.length() - 1));//长度7位
}
edt.setSelection(edt.getText().length());//不能用 text.length() 否则输入00索引异常
}
@Override
public void afterTextChanged(Editable s) {
}
};
}
输入 . 追加0
00开头替换为0
0开头并且第二位不是.的情况下去除开头的0