//此代码存在问题,请使用下方新版本
StringBuffer phoneNumSpace = new StringBuffer();
et_account.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) {
showError("");
phoneNumSpace.setLength(0);
phoneNumSpace.append(s);
int length = phoneNumSpace.length();//toString().length();
//删除数字
if (count == 0) {
if (length == 4) {
setContent(phoneNumSpace.deleteCharAt(3));
}
if (length == 9) {
setContent(phoneNumSpace.deleteCharAt(8));
}
}
//添加数字
if (count >= 1) {
if (length >= 4)
{
// char[] chars = s.toString().toCharArray();
//数字下标是从0开始
if (phoneNumSpace.charAt(3) != ' ')
{
phoneNumSpace.insert(3,' ');
setContent(phoneNumSpace);
}
}
if (length >= 9)
{
// char[] chars = s.toString().toCharArray();
//因为第4位加了一个空格,所以第8位数字,就是字符数组的第9位,下标是8。
if (phoneNumSpace.charAt(8) != ' ')
{
phoneNumSpace.insert(8,' ');
setContent(phoneNumSpace);
}
}
// if (length == 4) {
// String part1 = s.subSequence(0, 3).toString();
// String part2 = s.subSequence(3, length).toString();
// et_account.setText(part1 + " " + part2);
// }
// if (length==9){
//
// String part1 = s.subSequence(0, 8).toString();
// String part2 = s.subSequence(8, length).toString();
//
// et_account.setText(part1 + " " + part2);
// }
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
private void setContent(StringBuffer sb){
et_account.setText(sb.toString());
//移动光标到最后面
et_account.setSelection(sb.length());
}
由于上述代码在删除数字时出现问题,限制更新如下
et_account.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) {
showError("");
phoneNumSpace.setLength(0);
for (int i = 0;i<s.length();i++){
if (i != 3 && i != 8 && s.charAt(i) == ' '){
continue;
}else {
phoneNumSpace.append(s.charAt(i));
if ((phoneNumSpace.length() == 4 || phoneNumSpace.length() == 9) && phoneNumSpace.charAt(phoneNumSpace.length()-1) != ' '){
phoneNumSpace.insert(phoneNumSpace.length() - 1,' ');
}
}
}
if (!phoneNumSpace.toString().equals(s.toString())){
int index = start + 1;
if (phoneNumSpace.charAt(start) == ' '){
if (before == 0){
index ++;
}else {
index -- ;
}
}else {
if (before == 1){
index --;
}
}
et_account.setText(phoneNumSpace.toString());
et_account.setSelection(index);
if (count == 11){
et_account.setSelection(phoneNumSpace.length());
}
}
// phoneNumSpace.append(s);
// int length = phoneNumSpace.length();//toString().length();
// //删除数字
// if (count == 0) {
// if (length == 4) {
// setEtAccountContent(phoneNumSpace.deleteCharAt(3));
// }
// if (length == 9) {
// setEtAccountContent(phoneNumSpace.deleteCharAt(8));
// }
// }
// //添加数字
// if (count >= 1) {
// if (length >= 4)
// {
char[] chars = s.toString().toCharArray();
// //数字下标是从0开始
// if (phoneNumSpace.charAt(3) != ' ')
// {
// phoneNumSpace.insert(3,' ');
// setEtAccountContent(phoneNumSpace);
// }
// }
// if (length >= 9)
// {
char[] chars = s.toString().toCharArray();
// //因为第4位加了一个空格,所以第8位数字,就是字符数组的第9位,下标是8。
// if (phoneNumSpace.charAt(8) != ' ')
// {
// phoneNumSpace.insert(8,' ');
// setEtAccountContent(phoneNumSpace);
// }
// }
// if (length == 4) {
// String part1 = s.subSequence(0, 3).toString();
// String part2 = s.subSequence(3, length).toString();
// et_account.setText(part1 + " " + part2);
// }
// if (length==9){
//
// String part1 = s.subSequence(0, 8).toString();
// String part2 = s.subSequence(8, length).toString();
//
// et_account.setText(part1 + " " + part2);
// }
// }
if (TextUtils.isEmpty(s)){
iv_account_delete.setVisibility(View.GONE);
}else{
iv_account_delete.setVisibility(View.VISIBLE);
}
s = getPhoneNumClearSpace(s.toString());
if (LOGINTYPE == 0){
if (!TextUtils.isEmpty(s) ){
if (s.length() == 11){
if (ValidateUtil.validatePhone(s.toString())){
if (!TextUtils.isEmpty(et_password.getText().toString())){
bt_login_phone.setEnabled(true);
}else{
bt_login_phone.setEnabled(false);
}
}else{
bt_login_phone.setEnabled(false);
showError("请输入正确的手机号");
}
}else{
bt_login_phone.setEnabled(false);
}
}else{
bt_login_phone.setEnabled(false);
}
// if (!TextUtils.isEmpty(s) && s.length() == 11 && !ValidateUtil.validatePhone(s.toString())){
// bt_login_phone.setEnabled(false);
// showError("请输入正确的手机号");
// }
}
}
@Override
public void afterTextChanged(Editable s) {
}
});