android输入框EditText输入银行卡,输入手机,输入身份证格式化的实现

SpaceTextWatcher

android输入框输入银行卡,输入手机,输入身份证格式化的实现

项目地址

SpaceTextWatcher

实现方式

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
        beforeTextLength = s.length();
        if (buffer.length() > 0) {
            buffer.delete(0, buffer.length());
        }
        spaceNumberA = 0;
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == ' ') {
                spaceNumberA++;
            }
        }
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        onTextLength = s.length();
        buffer.append(s.toString());
        if (onTextLength == beforeTextLength || onTextLength > maxLenght
                || isChanged) {
            isChanged = false;
            return;
        }
        isChanged = true;
    }

    @Override
    public void afterTextChanged(Editable s) {
        if (isChanged) {
            location = editText.getSelectionEnd();
            int index = 0;
            while (index < buffer.length()) { // 删掉所有空格
                if (buffer.charAt(index) == ' ') {
                    buffer.deleteCharAt(index);
                } else {
                    index++;
                }
            }

            index = 0;
            int spaceNumberB = 0;
            while (index < buffer.length()) { // 插入所有空格
                spaceNumberB = insertSpace(index, spaceNumberB);
                index++;
            }

            String str = buffer.toString();

            // 下面是计算光位置的
            if (spaceNumberB > spaceNumberA) {
                location += (spaceNumberB - spaceNumberA);
                spaceNumberA = spaceNumberB;
            }
            if (isSetText) {
                location = str.length();
                isSetText = false;
            } else if (location > str.length()) {
                location = str.length();
            } else if (location < 0) {
                location = 0;
            }

            editText.setText(str);
            try {
                editText.setSelection(location);
            } catch (Exception e) {
                e.printStackTrace();
            }
            isChanged = false;
        }
    }

    /**
     * 根据类型插入空格
     * 
     * @param index
     * @param spaceNumberAfter
     * @return
     * @see [类、类#方法、类#成员]
     */
    private int insertSpace(int index, int spaceNumberAfter) {
        switch (spaceType) {
        case defaultType:// 相隔四位空格
            if (index > 3
                    && (index % (4 * (spaceNumberAfter + 1)) == spaceNumberAfter)) {
                buffer.insert(index, ' ');
                spaceNumberAfter++;
            }
            break;
        case bankCardNumberType:
            if (index > 3
                    && (index % (4 * (spaceNumberAfter + 1)) == spaceNumberAfter)) {
                buffer.insert(index, ' ');
                spaceNumberAfter++;
            }
            break;
        case mobilePhoneNumberType:
            if (index == 3
                    || ((index > 7) && ((index - 3) % (4 * spaceNumberAfter) == spaceNumberAfter))) {
                buffer.insert(index, ' ');
                spaceNumberAfter++;
            }
            break;
        case IDCardNumberType:
            if (index == 6
                    || ((index > 10) && ((index - 6) % (4 * spaceNumberAfter) == spaceNumberAfter))) {
                buffer.insert(index, ' ');
                spaceNumberAfter++;
            }
            break;
        default:
            if (index > 3
                    && (index % (4 * (spaceNumberAfter + 1)) == spaceNumberAfter)) {
                buffer.insert(index, ' ');
                spaceNumberAfter++;
            }
            break;
        }
        return spaceNumberAfter;
    }

    /***
     * 计算需要的空格数
     * 
     * @return 返回添加空格后的字符串长度
     * @see [类、类#方法、类#成员]
     */
    private int computeSpaceCount(CharSequence charSequence) {
        buffer.delete(0, buffer.length());
        buffer.append(charSequence.toString());
        int index = 0;
        int spaceNumberB = 0;
        while (index < buffer.length()) { // 插入所有空格
            spaceNumberB = insertSpace(index, spaceNumberB);
            index++;
        }
        buffer.delete(0, buffer.length());
        return index;
    }

使用方法

        AddSpaceTextWatcher[] asEditTexts=new AddSpaceTextWatcher[3];
        EditText[] editTexts=new EditText[3];
        editTexts[0]=(EditText)findViewById(R.id.editText);//银行卡
        editTexts[1]=(EditText)findViewById(R.id.editText2);//手机号
        editTexts[2]=(EditText)findViewById(R.id.editText3);//身份证
        asEditTexts[0]=new AddSpaceTextWatcher(editTexts[0],48);//银行卡
        asEditTexts[0].setSpaceType(AddSpaceTextWatcher.SpaceType.bankCardNumberType);
        asEditTexts[1]=new AddSpaceTextWatcher(editTexts[1],13);//手机号
        asEditTexts[1].setSpaceType(AddSpaceTextWatcher.SpaceType.mobilePhoneNumberType);
        asEditTexts[2]=new AddSpaceTextWatcher(editTexts[2],21);//身份证
        asEditTexts[2].setSpaceType(AddSpaceTextWatcher.SpaceType.IDCardNumberType);

关于作者Robin

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值