private void editTextListener(){
id_num.addTextChangedListener(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) {
if (s == null || s.length() == 0) return;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
if (i != 4 && i!= 9 && i != 14 && s.charAt(i) == ' ') {
continue;
} else {
sb.append(s.charAt(i));
if ((sb.length() == 7 || sb.length() == 12|| sb.length() == 17) && sb.charAt(sb.length() - 1) != ' ') {
sb.insert(sb.length() - 1, ' ');
}
}
}
if (!sb.toString().equals(s.toString())) {
int index = start + 1;
if (sb.charAt(start) == ' ') {
if (before == 0) {
index++;
} else {
id_num.setText(sb.subSequence(0, sb.length() - 1));
index--;
}
} else {
if (before == 1) {
index--;
}
}
id_num.setText(sb.toString());
id_num.setSelection(index);
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
原文链接:http://www.apkbus.com/blog-851511-75846.html
打开App,阅读手记