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(); if (len < getLength())// 长度小于要求的数 return; if (count > 1) { return; } //先倒置,运算之后再倒置回来 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()); } } } }
时间: 2024-12-12 12:03:50