1.自定義EditText控件 public class DivisionEditText extends EditText { /* 每組的長度 */ private Integer length = 3; /* 分隔符 */ private String delimiter = ","; private String text = ""; public DivisionEditText(Context context) { super(context); init(); } public DivisionEditText(Context context, AttributeSet attrs) { super(context, attrs); init(); } public DivisionEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } /** 獲得每組個數 */ public Integer getLength() { return length; } /** 設置每組個數 */ public void setLength(Integer length) { this.length = length; } /** 獲得間隔符 */ public String getDelimiter() { return delimiter; } /** 設置間隔符 */ public void setDelimiter(String delimiter) { this.delimiter = delimiter; } public String getInputText() { return super.getText().toString().replace(getDelimiter(), ""); } /** * 初始化 */ public void init() { // 內容變化監聽 this.addTextChangedListener(new DivisionTextWatcher()); // 獲取焦點監聽 this.setOnFocusChangeListener(new DivisionFocusChangeListener()); } /** * 文本監聽 * * @author Administrator * */ private class DivisionTextWatcher implements TextWatcher { @Override public void afterTextChanged(Editable s) { } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before,int count) { // 統計個數 int len = s.length(); // 輸入首字母爲0以後則不顯示 if (len > 1) { if (s.toString().charAt(0) == '0') { editText.setText("0"); setSelection(1); return; } } if (len < getLength())// 長度小於要求的數 return; if (count > 1) { return; } if (len > maxLength) {//限制輸入的長度 String str = editText.getText().toString(); // 截取新字符串 String newStr = str.substring(0, maxLength); text = inversionString(formatSymbol(inversionString(newStr))); maxListener.afterLengthMax();//自定義接口,實現監聽回調 } else { //先倒置,運算以後再倒置回來 text = inversionString(formatSymbol(inversionString(s.toString())));//關鍵點 } // text = inversionString(formatSymbol(inversionString(s.toString()))); setText(text); setSelection(text.length()); } } /** * 如有,先去除,進行計算以後再添加 */ private String formatSymbol(String str) { char[] chars = str.replace(getDelimiter(), "").toCharArray(); StringBuffer sb = new StringBuffer(); for (int i = 0; i < chars.length; i++) { if (i % getEachLength() == 0 && i != 0)// 每次遍歷到4的倍數,就添加一個空格 { sb.append(getDelimiter()); sb.append(chars[i]);// 添加字符 } else { sb.append(chars[i]);// 添加字符 } } return sb.toString(); } /** * 字符串逆序* * @param str * @return */ private String inversionString(String str) { char[] chars = str.toCharArray(); StringBuffer sb = new StringBuffer(); for(int i= 0; i < chars.length; i++) { sb.append(chars[chars.length - i - 1]); } return sb.toString(); } /** * 獲取焦點監聽 * * @author Administrator * */ private class DivisionFocusChangeListener implements OnFocusChangeListener { @Override public void onFocusChange(View v, boolean hasFocus) { if (hasFocus) { // 設置焦點 setSelection(getText().toString().length()); } } } /** EditText 長度最大化監聽 */ public interface OnChangeLengthMaxListener { public void afterLengthMax(); } public void setOnChangeLengthMaxListener(OnChangeLengthMaxListener maxListener) { this.maxListener = maxListener; } private OnChangeLengthMaxListener maxListener; }