需求:注冊(cè)界面輸入手機(jī)號(hào)時(shí)候以188 8888 8888形式呈現(xiàn)
感謝博主:https://blog.csdn.net/as_csdn
實(shí)現(xiàn)效果:
實(shí)現(xiàn)EditText的TextWatcher接口即可矩欠,具體在深入研究
et_phone.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
DLog.d("---------- beforeTextChanged ----------" + "\n");
DLog.d("s: " + s + "\n");
DLog.d("start: " + start + "\n");
DLog.d("count: " + count + "\n");
DLog.d("after: " + after + "\n");
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
DLog.d("---------- onTextChanged ----------" + "\n");
DLog.d("s: " + s + "\n");
DLog.d("start: " + start + "\n");
DLog.d("before: " + before + "\n");
DLog.d("count: " + count + "\n");
if (s==null||s.length()==0){
return;
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i <s.length() ; i++) {
if (i != 3 && i != 8 && s.charAt(i) == ' ') {
continue;
} else {
sb.append(s.charAt(i));
if ((sb.length() == 4 || sb.length() == 9)
&& 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 {
index--;
}
} else {
if (before == 1) {
index--;
}
}
et_phone.setText(sb.toString());
et_phone.setSelection(index);
}
}
@Override
public void afterTextChanged(Editable s) {
DLog.d("---------- afterTextChanged ----------" + "\n");
DLog.d("s: " + s + "\n");
}
});
后期在更新 相關(guān).....