view包:
import android.content.Context;
import android.text.Editable;
import android.text.Selection;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.widget.EditText;
public class MoblieEditText extends EditText {
public MoblieEditText(Context context) {
super(context);
this.addTextChangedListener(new MobileWatcher());
}
public MoblieEditText(Context context, AttributeSet attrs) {
super(context, attrs);
this.addTextChangedListener(new MobileWatcher());
}
public MoblieEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.addTextChangedListener(new MobileWatcher());
}
class MobileWatcher implements TextWatcher {
int beforeTextLength = 0;
int onTextLength = 0;
boolean isChanged = false;
int location = 0;// 记录光标的位置
private char[] tempChar;
private final StringBuffer buffer = new StringBuffer();
int konggeNumberB = 0;
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
beforeTextLength = s.length();
if (buffer.length() > 0) {
buffer.delete(0, buffer.length());
}
konggeNumberB = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == ' ') {
konggeNumberB++;
}
}
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
onTextLength = s.length();
buffer.append(s.toString());
if (onTextLength == beforeTextLength || onTextLength <= 3 || isChanged) {
isChanged = false;
return;
}
isChanged = true;
}
@Override
public void afterTextChanged(Editable s) {
if (isChanged) {
location = getSelectionEnd();
int index = 0;
while (index < buffer.length()) {
if (buffer.charAt(index) == ' ') {
buffer.deleteCharAt(index);
} else {
index++;
}
}
index = 0;
int konggeNumberC = 0;
while (index < buffer.length()) {
if ((index == 3 || index == 8)) {
buffer.insert(index, ' ');
konggeNumberC++;
}
index++;
}
if (konggeNumberC > konggeNumberB) {
location += (konggeNumberC - konggeNumberB);
}
tempChar = new char[buffer.length()];
buffer.getChars(0, buffer.length(), tempChar, 0);
String str = buffer.toString();
if (location > str.length()) {
location = str.length();
} else if (location < 0) {
location = 0;
}
setText(str);
Editable etable = getText();
Selection.setSelection(etable, location);
isChanged = false;
}
}
}
}
布局之一:
在activity中初始化:
etPhone.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) {
btnSignUp.setBackgroundResource(R.drawable.shape_sign_up_button_pre);
}
@Override
public void afterTextChanged(Editable s) {
if (etPhone.getText().toString().trim().equals("")) {
btnSignUp.setBackgroundResource(R.drawable.shape_sign_up_button);
} else {
btnSignUp.setBackgroundResource(R.drawable.shape_sign_up_button_pre);
}
if (s != null && s.length() == 13) {
if (etPhone.isFocused()) {
etPhone.clearFocus();
etPsw.requestFocus();
}
}
}
});