Format credit card in edit text in android

后端 未结 29 2039
耶瑟儿~
耶瑟儿~ 2020-11-30 19:18

How to make EditText accept input in format:

4digit 4digit 4digit 4digit 

I tried Custom format edit text input android to acc

29条回答
  •  情歌与酒
    2020-11-30 19:39

    Here's a simple and easily customizable solution using the TextWatcher class. It may be assigned to your EditText using the addTextChangedListener() method.

    new TextWatcher() {
        /** Formats the Field to display user-friendly separation of the input values. */
        @Override public final void afterTextChanged(final Editable pEditable) {
            // Declare the separator.
            final char lSeparator      = '-';
            // Declare the length of separated text. i.e. (XXXX-XXXX-XXXX)
            final int  lSeparationSize = 4;
            // Declare the count; tracks the number of allowed characters in a row.
                  int lCount          = 0;
            // Iterate the Characters.
            for(int i = 0; i < pEditable.length(); i++) {
                // Fetch the current character.
                final char c              = pEditable.charAt(i);
                // Is it a usual character. Here, we permit alphanumerics only.
                final boolean lIsExpected = (Character.isDigit(c) || Character.isLetter(c)) && (c != lSeparator);
                // Is the character expected?
                if(lIsExpected) {
                    // Increase the count.
                    lCount++;
                }
                else {
                    // Is it a separator?
                    if(c == lSeparator) {
                        // Reset the count.
                        lCount = 0;
                        // Continue the iteration.
                        continue;
                    }
                }
                // Has the count been exceeded? Is there more text coming?
                if(lCount >= (lSeparationSize + 1) && (i < pEditable.length())) {
                    // Reset the count.
                    lCount = 0;
                    // Insert the separator.
                    pEditable.insert(i, Character.toString(lSeparator));
                    // Increase the iteration count.
                    i++;
                }
            }
        }
        /** Unused overrides. */
        @Override public final void beforeTextChanged(final CharSequence pCharSequence, final int pStart, final int pCount, final int pAfter) { }
        @Override public final void onTextChanged(final CharSequence pCharSequence, final int pStart, final int pBefore, final int pCount) { }
    }
    

    Alternatively, here is a much cleaner implementation based on epool's implementation.

    public final class TextGroupFormattingListener implements TextWatcher {
    
        /* Member Variables. */
        private final int    mGroupLength;
        private final String mSeparator;
        private       String mSource;
    
        /** Constructor. */
        public TextGroupFormattingListener(final String pSeparator, final int pGroupLength) {
            // Initialize Member Variables.
            this.mSeparator   = pSeparator;
            this.mGroupLength = pGroupLength;
            this.mSource      = "";
        }
    
        /** Formats the Field to display user-friendly separation of the input values. */
        @Override public final void afterTextChanged(final Editable pEditable) {
            // Fetch the Source.
            String lSource = pEditable.toString();
            // Has the text changed?
            if (!this.getSource().equals(lSource)) {
                // Remove all of the existing Separators.
                lSource = lSource.replace(this.getSeparator(), "");
                // Allocate a StringBuilder.
                StringBuilder lStringBuilder = new StringBuilder();
                // Iterate across the Source String, which contains the raw user input.
                for(int i = 0; i < lSource.length(); i++) {
                    // Have we exceeded the GroupLength?
                    if(i > 0 && i % this.getGroupLength() == 0) {
                        // Append the separator.
                        lStringBuilder.append(this.getSeparator());
                    }
                    // Append the user's character data.
                    lStringBuilder.append(lSource.charAt(i));
                }
                // Track changes to the Source.
                this.setSource(lStringBuilder.toString());
                // Replace the contents of the Editable with this new String.
                pEditable.replace(0, pEditable.length(), this.getSource());
            }
        }
    
        /** Unused overrides. */
        @Override public final void beforeTextChanged(final CharSequence pCharSequence, final int pStart, final int pCount, final int pAfter) { }
        @Override public final void onTextChanged(final CharSequence pCharSequence, final int pStart, final int pBefore, final int pCount)    { }
    
        public final int getGroupLength() {
            return this.mGroupLength;
        }
    
        public final String getSeparator() {
            return this.mSeparator;
        }
    
        private final void setSource(final String pSource) {
            this.mSource = pSource;
        }
    
        private final String getSource() {
            return this.mSource;
        }
    
    }
    

提交回复
热议问题