Bank card formatting (every four spaces, delete add, change, etc.)

Dynamically format the bank card when entering

1. At present, it is known that there will be some bugs in long-pressing and pasting. It has not been solved for the time being. The unified processing is to put the cursor at the end.

2. Can not long press to delete, guess is format(String. valueOf (editable)); After the method, the focus of the delete button disappears, and it has not been solved for the time being.

/**
 * Select one and paste one (the cursor stays at the last of the pasted element)
 * Select one for deletion (the cursor stays at the start position of the deleted element)
 * Select one and paste multiple (the cursor stays at the last of the pasted element)
 *
 * Select multiple pastes the same amount (the cursor stays at the last of the pasted element)
 * Select multiple and paste a small amount (the cursor stays at the last of the pasted element)
 * Select multiple and paste more (the cursor stays at the last of the pasted element)
 */

/**
 * Created by sqj on 2017/12/5.
 */

public class BankIDFormatEditText extends EditText {
    String space = " ";
    private boolean shouldStopChange = false;
    private int textOnViewLength;
    private String mTextBefore;
    private String mTextAfter;
    private int mStartPosition;
    private int mEndPosition;

    public BankIDFormatEditText(Context context) {
        this(context, null);
    }

    public BankIDFormatEditText(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BankIDFormatEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        setFocusable(true);
        setEnabled(true);
        setFocusableInTouchMode(true);
        addTextChangedListener(new BandCardWatcher());

    }

    private class BandCardWatcher implements TextWatcher {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            if (shouldStopChange) {
            } else {
                mTextBefore = String.valueOf(s);
                mStartPosition = getSelectionStart();
                mEndPosition = getSelectionEnd();
                // The total length after the first change (hand lose)
                mBeforeChangeLength = s.toString().length();

            }
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
//            Log.d("BandCardWatcher", "onTextChanged:" + s);
        }

        @Override
        public void afterTextChanged(Editable editable) {
            if (shouldStopChange) {
                shouldStopChange = false;
                return;
            }
            shouldStopChange = true;

            mTextAfter = String.valueOf(editable);
            if (mStartPosition!=mEndPosition){// This is the state selected by long press
                format(editable);
                setSelection (textOnViewLength);
            }else {
                if (mTextBefore.length() < mTextAfter.length()) {// 增加

                    format(editable);
                    if ((mStartPosition + 1) % 5 == 0) {
                        if (mStartPosition != 0) {
                            setSelection(mStartPosition + 2);
                        }
                    } else {
                        setSelection(mStartPosition + 1);
                    }
                } else if (mTextBefore.length() > mTextAfter.length()) { // 减少

                    if (mStartPosition % 5 == 0) {
                        if (mStartPosition != 0) {
                            // When the cursor is behind the space, click delete
                            editable.replace(mStartPosition - 2, mStartPosition - 1, " ");
                        }
                    }

                    format(editable);
                    if (mStartPosition != 1 && (mStartPosition - 1) % 5 == 0) {
                        setSelection(mStartPosition - 2);
                    } else if (mStartPosition != 0) {
                        setSelection(mStartPosition - 1);
                    }


                } else if (mTextBefore.length() == mTextAfter.length()) {// 替换

                    format(editable);
                    setSelection(mStartPosition);
                }
            }



        }
    }


    private void format(Editable editable) {

        String str = editable.toString().trim().replaceAll(space, "");
        int len = str.length();
        int courPos;

        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < len; i++) {

            if (i % 4 == 0 && i != 0) {
                builder.append(space);
                builder.append(str.charAt(i));
            } else {
                builder.append(str.charAt(i));
            }
        }
        courPos = builder.length();
        textOnViewLength = builder.toString().length();
        setText(builder.toString());


    }

}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325732066&siteId=291194637