【android】EditText输入银行卡号每四位空一格

本文介绍了一种仿照微信银行卡号输入方式的实现方法,通过自动在每四位数字后添加空格,使得输入的银行卡号更加清晰易读。文章提供了完整的Android代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

完美仿微信银行卡号输入,输入每4个数字就自动添加一个空格

代码如下:

import android.text.Editable;
import android.text.Selection;
import android.text.TextWatcher;
import android.widget.EditText;

/**
 * Bank card input TextWatcher
 * 4 grouping input
 * Please set EditText max length is 26
 * Created by cc_want on 2017/7/13.
 */
public class BankCardTextWatcher implements TextWatcher {

    //default max length = 21 + 5 space
    private static final int DEFAULT_MAX_LENGTH = 21 + 5;
    //max input length
    private int maxLength = DEFAULT_MAX_LENGTH;
    private int beforeTextLength = 0;
    private boolean isChanged = false;

    //space count
    private int space = 0;

    private StringBuffer buffer = new StringBuffer();
    private EditText editText;

    public static void bind(EditText editText){
        new BankCardTextWatcher(editText,DEFAULT_MAX_LENGTH);
    }
    public static void bind(EditText editText,int maxLength){
        new BankCardTextWatcher(editText,maxLength);
    }
    public BankCardTextWatcher(EditText editText,int maxLength){
        this.editText = editText;
        this.maxLength = maxLength;
        editText.addTextChangedListener(this);
    }
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        beforeTextLength = s.length();
        if (buffer.length() > 0) {
            buffer.delete(0, buffer.length());
        }
        space = 0;
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == ' ') {
                space ++;
            }
        }
    }
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        int length = s.length();
        buffer.append(s.toString());
        if (length == beforeTextLength || length <= 3
                || isChanged) {
            isChanged = false;
            return;
        }
        isChanged = true;
    }
    @Override
    public void afterTextChanged(Editable s) {
        if (isChanged) {
            int selectionIndex = editText.getSelectionEnd();
            //total char length
            int index = 0;
            while (index < buffer.length()) {
                if (buffer.charAt(index) == ' ') {
                    buffer.deleteCharAt(index);
                } else {
                    index ++;
                }
            }
            //total space count
            index = 0;
            int totalSpace = 0;
            while (index < buffer.length()) {
                if ((index == 4 || index == 9 || index == 14 || index == 19 || index == 24)) {
                    buffer.insert(index, ' ');
                    totalSpace ++;
                }
                index++;
            }
            //selection index
            if (totalSpace > space) {
                selectionIndex += (totalSpace - space);
            }
            char[] tempChar = new char[buffer.length()];
            buffer.getChars(0, buffer.length(), tempChar, 0);
            String str = buffer.toString();
            if (selectionIndex > str.length()) {
                selectionIndex = str.length();
            } else if (selectionIndex < 0) {
                selectionIndex = 0;
            }
            editText.setText(str);
            Editable text = editText.getText();
            //set selection
            Selection.setSelection(text, selectionIndex < maxLength ? selectionIndex : maxLength);
            isChanged = false;
        }
    }
}

一般银行卡号最长21位,再加上中间的5个空格,所以我们设置EditText的最大可输入长度为26

使用方法如下:

1.设置EditText的最大可输入长度为26

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="请输入您的卡号"
    android:inputType="number"
    android:maxLength="26"
    android:singleLine="true" />  

2.添加EditText输入监听

BankCardTextWatcher.bind(mEdtBankCardNo);


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值