Custom format edit text input android to accept credit card number

后端 未结 8 887
终归单人心
终归单人心 2020-11-29 01:46

how to make edit text accept input in format

4digitnumber-4dignumber-4dignumber-4dignumber   

The code

text.addTextChangedL         


        
8条回答
  •  误落风尘
    2020-11-29 02:51

    If you want to just group visually the numbers, but you don't want to alter the value of the EditText adding dashes, you can use this Span approach:

    EditText editText = findViewById(R.id.editText);
    editText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
    
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {}
    
        @Override
        public void afterTextChanged(Editable editable) {
            Object[] paddingSpans = editable.getSpans(0, editable.length(), DashSpan.class);
            for (Object span : paddingSpans) {
                editable.removeSpan(span);
            }
    
            addSpans(editable);
        }
    
        private static final int GROUP_SIZE = 4;
    
        private void addSpans(Editable editable) {
    
            final int length = editable.length();
            for (int i = 1; i * (GROUP_SIZE) < length; i++) {
                int index = i * GROUP_SIZE;
                editable.setSpan(new DashSpan(), index - 1, index,
                        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }
    });
    

    where the DashSpan class looks like this:

    /**
     * A {@link ReplacementSpan} used for spacing in {@link android.widget.EditText}
     * to space things out. Adds '-'s
     */
    public class DashSpan extends ReplacementSpan {
    
        @Override
        public int getSize(@NonNull Paint paint, CharSequence text, int start, int end, FontMetricsInt fm) {
            float padding = paint.measureText("-", 0, 1);
            float textSize = paint.measureText(text, start, end);
            return (int) (padding + textSize);
        }
    
        @Override
        public void draw(@NonNull Canvas canvas, CharSequence text, int start, int end, float x, int top, int y,
                         int bottom, @NonNull Paint paint) {
            canvas.drawText(text.subSequence(start, end) + "-", x, y, paint);
        }
    }
    

    This way you will have visually the grouping using the dashes, but the getText() will return the text without any grouping.

    To force only numbers you can add the attributes android:digits="0123456789" and android:inputType="number" to the EditText.

    This solution is based on the code of this library.

提交回复
热议问题