美文网首页银行卡
Android银行卡输入框与卡号显示

Android银行卡输入框与卡号显示

作者: 他一生嫉恶如仇丶 | 来源:发表于2017-01-06 08:32 被阅读169次

关于一个支付项目用到的,银行卡输入框和显示的TextView。
银行卡输入框,四位一组。
自定义一个BankCardNumEditText继承EditText,代码如下:

public class BankCardNumEditText extends EditText {

    public BankCardNumEditText(Context context) {
        super(context);
        this.addTextChangedListener(new BankCardNumWatcher());
    }

    public BankCardNumEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.addTextChangedListener(new BankCardNumWatcher());
    }

    /**
     * 获取真实的text(去掉空格)
     *
     * @return
     */
    public String getTextWithoutSpace() {
        String text = super.getText().toString();
        if (android.text.TextUtils.isEmpty(text)) {
            return "";
        } else {
            return text.replace(" ", "");
        }
    }

    /**
     * 银行卡号输入框格式(每4位有个空格)
     *
     * @author Administrator
     */
    class BankCardNumWatcher implements 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 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 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 afterTextChanged(Editable s) {
            if (isChanged) {
                location = 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;
                }
                setText(str);
                Editable etable = getText();
                Selection.setSelection(etable, location);
                isChanged = false;
            }
        }
    }
}

相关文章

网友评论

    本文标题:Android银行卡输入框与卡号显示

    本文链接:https://www.haomeiwen.com/subject/owetbttx.html