银行卡四位加空格

代码说话吧:

  /**
     * 
     * TODO<银行卡四位加空格>
     * @param et_bankcard
     * @throw
     * @return void
     */
    protected void bankCardNumAddSpace(final ClearableEditText et_bankcard) {
        et_bankcard.addTextChangedListener(new TextWatcher() {
            int beforeTextLength = 0;
            
            int onTextLength = 0;
            
            boolean isChanged = false;
            
            int location = 0;// 记录光标的位置
            
            private char[] tempChar;
            
            private StringBuffer buffer = new StringBuffer();
            
            int konggeNumberB = 0;
            
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
                beforeTextLength = s.length();
                if (buffer.length() > 0) {
                    buffer.delete(0, buffer.length());
                }
                konggeNumberB = 0;
                for (int i = 0; i < s.length(); i++) {
                    if (s.charAt(i) == ' ') {
                        konggeNumberB++;
                    }
                }
                
            }
            
            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                int count) {
                
                onTextLength = s.length();
                buffer.append(s.toString());
                if (onTextLength == beforeTextLength || onTextLength <= 3
                    || isChanged) {
                    isChanged = false;
                    return;
                }
                isChanged = true;
                
            }
            
            @Override
            public void afterTextChanged(Editable s) {
                if (isChanged) {
                    location = et_bankcard.getSelectionEnd();
                    int index = 0;
                    while (index < buffer.length()) {
                        if (buffer.charAt(index) == ' ') {
                            buffer.deleteCharAt(index);
                        } else {
                            index++;
                        }
                    }
                    
                    index = 0;
                    int konggeNumberC = 0;
                    while (index < buffer.length()) {
                        if ((index == 4 || index == 9 || index == 14 || index == 19)) {
                            buffer.insert(index, ' ');
                            konggeNumberC++;
                        }
                        index++;
                    }
                    
                    if (konggeNumberC > konggeNumberB) {
                        location += (konggeNumberC - konggeNumberB);
                    }
                    
                    tempChar = new char[buffer.length()];
                    buffer.getChars(0, buffer.length(), tempChar, 0);
                    String str = buffer.toString();
                    if (location > str.length()) {
                        location = str.length();
                    } else if (location < 0) {
                        location = 0;
                    }
                    
                    et_bankcard.setText(str);
                    Editable etable = et_bankcard.getText();
                    Selection.setSelection(etable, location);
                    isChanged = false;
                }
                
            }
        });
    }
    


 

 et_bankcard = (ClearableEditText)findViewById(R.id.et_bankcard);
        et_bankcard.setFilters(new InputFilter[] {new InputFilter.LengthFilter(
            23)});

  bankCardNumAddSpace(et_bankcard);


注释:ClearableEditText这个为自定义EditText控件,可以直接套用原生的。

核心逻辑是套用:chinavfeng

java思想:不要重复的发明轮子~~~

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值