Android edittext 输入手机号自动添加空格,删除时自动清除空格(更新2022年8月23日)

//此代码存在问题,请使用下方新版本
StringBuffer phoneNumSpace = new StringBuffer();
et_account.addTextChangedListener(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) {
                showError("");

                phoneNumSpace.setLength(0);
                phoneNumSpace.append(s);
                int length = phoneNumSpace.length();//toString().length();
                //删除数字
                if (count == 0) {
                    if (length == 4) {
                        setContent(phoneNumSpace.deleteCharAt(3));
                    }
                    if (length == 9) {
                        setContent(phoneNumSpace.deleteCharAt(8));
                    }
                }
                //添加数字
                if (count >= 1) {
                    if (length >= 4)
                    {
//                        char[] chars = s.toString().toCharArray();
                        //数字下标是从0开始
                        if (phoneNumSpace.charAt(3) != ' ')
                        {
                            phoneNumSpace.insert(3,' ');
                            setContent(phoneNumSpace);
                        }
                    }
                    if (length >= 9)
                    {
//                        char[] chars = s.toString().toCharArray();
                        //因为第4位加了一个空格,所以第8位数字,就是字符数组的第9位,下标是8。
                        if (phoneNumSpace.charAt(8) != ' ')
                        {
                            phoneNumSpace.insert(8,' ');
                            setContent(phoneNumSpace);
                        }
                    }

//                    if (length == 4) {
//                        String part1 = s.subSequence(0, 3).toString();
//                        String part2 = s.subSequence(3, length).toString();
//                        et_account.setText(part1 + " " + part2);
//                    }
//                    if (length==9){
//
//                        String part1 = s.subSequence(0, 8).toString();
//                        String part2 = s.subSequence(8, length).toString();
//
//                        et_account.setText(part1 + " " + part2);
//                    }
                }




            }

            @Override
            public void afterTextChanged(Editable s) {
               
            }
        });


private void setContent(StringBuffer sb){
        et_account.setText(sb.toString());
        //移动光标到最后面
        et_account.setSelection(sb.length());
    }

由于上述代码在删除数字时出现问题,限制更新如下

et_account.addTextChangedListener(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) {
                showError("");


                phoneNumSpace.setLength(0);
                for (int i = 0;i<s.length();i++){
                    if (i != 3 && i != 8 && s.charAt(i) == ' '){
                        continue;
                    }else {
                        phoneNumSpace.append(s.charAt(i));
                        if ((phoneNumSpace.length() == 4 || phoneNumSpace.length() == 9) && phoneNumSpace.charAt(phoneNumSpace.length()-1) != ' '){
                            phoneNumSpace.insert(phoneNumSpace.length() - 1,' ');
                        }
                    }
                }

                if (!phoneNumSpace.toString().equals(s.toString())){
                    int index = start + 1;
                    if (phoneNumSpace.charAt(start) == ' '){
                        if (before == 0){
                            index ++;
                        }else {
                            index -- ;
                        }
                    }else {
                        if (before == 1){
                            index --;
                        }
                    }
                    et_account.setText(phoneNumSpace.toString());
                    et_account.setSelection(index);
                    if (count == 11){
                        et_account.setSelection(phoneNumSpace.length());
                    }
                }

//                phoneNumSpace.append(s);
//                int length = phoneNumSpace.length();//toString().length();
//                //删除数字
//                if (count == 0) {
//                    if (length == 4) {
//                        setEtAccountContent(phoneNumSpace.deleteCharAt(3));
//                    }
//                    if (length == 9) {
//                        setEtAccountContent(phoneNumSpace.deleteCharAt(8));
//                    }
//                }
//                //添加数字
//                if (count >= 1) {
//                    if (length >= 4)
//                    {
                        char[] chars = s.toString().toCharArray();
//                        //数字下标是从0开始
//                        if (phoneNumSpace.charAt(3) != ' ')
//                        {
//                            phoneNumSpace.insert(3,' ');
//                            setEtAccountContent(phoneNumSpace);
//                        }
//                    }
//                    if (length >= 9)
//                    {
                        char[] chars = s.toString().toCharArray();
//                        //因为第4位加了一个空格,所以第8位数字,就是字符数组的第9位,下标是8。
//                        if (phoneNumSpace.charAt(8) != ' ')
//                        {
//                            phoneNumSpace.insert(8,' ');
//                            setEtAccountContent(phoneNumSpace);
//                        }
//                    }

//                    if (length == 4) {
//                        String part1 = s.subSequence(0, 3).toString();
//                        String part2 = s.subSequence(3, length).toString();
//                        et_account.setText(part1 + " " + part2);
//                    }
//                    if (length==9){
//
//                        String part1 = s.subSequence(0, 8).toString();
//                        String part2 = s.subSequence(8, length).toString();
//
//                        et_account.setText(part1 + " " + part2);
//                    }
//                }






                if (TextUtils.isEmpty(s)){
                    iv_account_delete.setVisibility(View.GONE);
                }else{
                    iv_account_delete.setVisibility(View.VISIBLE);
                }
                s = getPhoneNumClearSpace(s.toString());
                if (LOGINTYPE == 0){
                    if (!TextUtils.isEmpty(s) ){
                        if (s.length() == 11){
                            if (ValidateUtil.validatePhone(s.toString())){
                                if (!TextUtils.isEmpty(et_password.getText().toString())){
                                    bt_login_phone.setEnabled(true);
                                }else{
                                    bt_login_phone.setEnabled(false);
                                }
                            }else{
                                bt_login_phone.setEnabled(false);
                                showError("请输入正确的手机号");
                            }
                        }else{
                            bt_login_phone.setEnabled(false);
                        }
                    }else{
                        bt_login_phone.setEnabled(false);
                    }


//                    if (!TextUtils.isEmpty(s) && s.length() == 11 && !ValidateUtil.validatePhone(s.toString())){
//                        bt_login_phone.setEnabled(false);
//                        showError("请输入正确的手机号");
//                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值