开发阶段遇到一需求:输入里程数,里程可以是0,但是不能是以0开头的其他数。代码如下:
private class CustomTextWatcher implements 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) {
String content = s.toString().trim();
if (content.length() > 1 && content.startsWith("0")){
String newContent = content.replace("0","");
editText.setText(newContent);
editText.setSelection(newContent.length());
}
}
}
网友评论