• 自定义输入手机号自动添加分隔符


    比较简单的一个控件,就是加些逻辑处理而已,以前貌似是直接监听的,封装起来方便点

    public class AccountTxtView extends android.support.v7.widget.AppCompatEditText {
    
        private final char CUT = '-';
    
        public AccountTxtView(Context context) {
            super(context);
        }
    
        public AccountTxtView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public AccountTxtView(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }
    
        @Override
        protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
            if (text == null || text.length() == 0)
                return;
            StringBuilder sb = new StringBuilder();
    
            for (int i = 0; i < text.length(); i++) {//添加分割符
                if (i != 3 && i != 8 && text.charAt(i) == CUT) {
                    continue;
                } else {
                    sb.append(text.charAt(i));
                    if ((sb.length() == 4 || sb.length() == 9)
                            && sb.charAt(sb.length() - 1) != CUT) {
                        sb.insert(sb.length() - 1, CUT);
                    }
                }
            }
            //防止多次设置值
            if (!sb.toString().equals(text.toString())) {
                int index = start + 1;
                if (sb.charAt(start) == CUT) {
                    if (lengthBefore == 0) {
                        index++;
                    } else {
                        index--;
                    }
                } else {
                    if (lengthBefore == 1) {
                        index--;
                    }
                }
                setText(sb.toString());
                setSelection(index);
            }else{//删除时候判断
                String line = text.subSequence(text.length() - 1, text.length()).toString();
                if (line.equals(String.valueOf(CUT))) {//如果删除碰到‘-’符号,则默认去除
                    sb.deleteCharAt(text.subSequence(0, text.length() - 1).length());
                    setText(sb.toString());
                    setSelection(sb.length());
                }
            }
        }
    
        public String getPhone() {
            String result = null;
            String val = getText().toString();
            if (val == null || val.isEmpty())
                return "";
            result = val.replace(String.valueOf(CUT), "");
            return result;
        }
    }
    View Code

    使用也简单

    然后效果就出来了

    输入第四个的时候自动填充‘-’分隔符,然后删除的时候自动删除‘-’分隔符

  • 相关阅读:
    POJ1258——Prim——Agri-Net
    POJ2485——Prim——Highways
    POJ1789——Prim——Truck History
    POJ1125——Floyed——Stockbroker Grapevine
    POJ2253——Floyed——Frogger
    字符串处理 Codeforces Round #297 (Div. 2) B. Pasha and String
    模拟 Codeforces Round #297 (Div. 2) A. Vitaliy and Pie
    DP+埃氏筛法 Codeforces Round #304 (Div. 2) D. Soldier and Number Game
    queue+模拟 Codeforces Round #304 (Div. 2) C. Soldier and Cards
    贪心 Codeforces Round #304 (Div. 2) B. Soldier and Badges
  • 原文地址:https://www.cnblogs.com/LiuZhen/p/6869358.html
Copyright © 2020-2023  润新知