/**
* @author CatDog
*
* @功能描述:格式化输入手机号码3-4-4
*/
public class PhoneFormatTextWatcher implements TextWatcher {
private int oldLength; // 用于对比,否则输入到3或者8位时无法删除
private int curLength;
public void afterTextChanged(Editable editable) {
if (editable != null) {
if (curLength > oldLength) {
if ((curLength == 4 || curLength == 9)) {
editable.insert(oldLength, " ");
}
} else { // 删除了
if (oldLength == 5 || oldLength == 10) {
editable.delete(curLength - 1, curLength);
}
}
}
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
curLength = s.length();
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
oldLength = s.length();
}
}
Andriod手机号码3-4-4输入
最新推荐文章于 2023-12-28 19:24:12 发布