手机号格式化(EditText空格输入)

/**
* zxw
* 手机号格式化。(中间删除,尾部删除,复制粘贴,依然保留分割的样子,前面有空格的话一并删除)
*/
public class ContentWithSpaceEditText extends android.support.v7.widget.AppCompatEditText {
private int maxLength = 100;
private int start, count, before;
private String digits;
public ContentWithSpaceEditText(Context context) {
this(context, null);
}
public ContentWithSpaceEditText(Context context, AttributeSet attrs) {
super(context, attrs);
parseAttributeSet(context, attrs);

}
public ContentWithSpaceEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
parseAttributeSet(context, attrs);
}
private void parseAttributeSet(Context context, AttributeSet attrs) {
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.ContentWithSpaceEditText, 0, 0);
ta.recycle();
initType();
setSingleLine();
addTextChangedListener(watcher);
}
private void initType() {
maxLength = 13;
digits = “0123456789 “;
setInputType(InputType.TYPE_CLASS_NUMBER);
setFilters(new InputFilter[]{new InputFilter.LengthFilter(maxLength)});
}

@Override
public void setInputType(int type) {
super.setInputType(type);
if (!TextUtils.isEmpty(digits)) {
setKeyListener(DigitsKeyListener.getInstance(digits));
}
}
private TextWatcher watcher = 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) {
ContentWithSpaceEditText.this.start = start;
ContentWithSpaceEditText.this.before = before;
ContentWithSpaceEditText.this.count = count;
LogUtils.i(“====>”+count);
}

@Override
public void afterTextChanged(Editable s) {
  if (s == null) {
    return;
  }
  //判断是否是在中间输入,需要重新计算
  boolean isMiddle = (start + count) < (s.length());
  //在末尾输入时,是否需要加入空格
  boolean isNeedSpace = false;
  if (!isMiddle && isSpace(s.length())) {
    isNeedSpace = true;
  }
  if (isMiddle || isNeedSpace || count > 1) {
    String newStr = s.toString();
    newStr = newStr.replace(" ", "");
    StringBuilder sb = new StringBuilder();
    int spaceCount = 0;
    for (int i = 0; i < newStr.length(); i++) {
      sb.append(newStr.substring(i, i + 1));
      //如果当前输入的字符下一位为空格(i+1+1+spaceCount),因为i是从0开始计算的,所以一开始的时候需要先加1
      if (isSpace(i + 2 + spaceCount)) {
        sb.append(" ");
        spaceCount += 1;
      }
    }
    removeTextChangedListener(watcher);
    s.replace(0, s.length(), sb);
    //如果是在末尾的话,或者加入的字符个数大于零的话(输入或者粘贴)
    if(!isMiddle){
      if(count > 1){
        setSelection(s.length() <= maxLength ? s.length() : maxLength);
      }
      if (count == 0) {
        //如果删除时,光标停留在空格的前面,光标则要往前移一位
        if (isSpace(start - before + 1)) {
          setSelection((start - before) > 0 ? start - before : 0);
        } else {
          setSelection((start - before + 1) > s.length() ? s.length() : (start - before + 1));
        }
      }
    }
    else if (isMiddle) {
      //如果是删除
      if (count == 0) {
        //如果删除时,光标停留在空格的前面,光标则要往前移一位
        if (isSpace(start - before + 1)) {
          setSelection((start - before) > 0 ? start - before : 0);
        } else {
          setSelection((start - before + 1) > s.length() ? s.length() : (start - before + 1));
        }
      }
      //如果是增加
      else {
        if (isSpace(start - before + count)) {
          setSelection((start + count - before + 1) < s.length() ? (start + count - before + 1) : s.length());
        } else {
          setSelection(start + count - before);
        }
      }
    }
    addTextChangedListener(watcher);
  }
}

};
private boolean isSpace(int length) {
return isSpacePhone(length);
}
private boolean isSpacePhone(int length) {
return length >= 4 && (length == 4 || (length + 1) % 5 == 0);
}
}

撸的别人的代码,不过他的有点小瑕疵,改了改。
通道:http://blog.csdn.net/my_rabbit/article/details/69946651

你可能感兴趣的:(Android,手机格式化,SpaceEditT,手机号中间空格分开)