1. 程式人生 > >手機號格式化(EditText空格輸入)

手機號格式化(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);
}
}