MaskedEditText icon indicating copy to clipboard operation
MaskedEditText copied to clipboard

Can't set initial text

Open Neznakomec opened this issue 8 years ago • 1 comments

Hi, can you implement a public method to set initial text? setText(CharSequence text) not working properly

I wanna to do something like this: maskedEditText.setMask("(###)###"); maskedEditText.setRawText("(124)343"); cause I'm saving editText content to string and then need to restore masked edittext from string.

Method should check that length of given text is equals to mask length and then replace all the mask[i] symbols where character '#' is placed with text[i] symbols

Thank you

Neznakomec avatar Apr 29 '16 18:04 Neznakomec

I tried to implement such a method by myself and wrote a code

    public void provideText(final String text) {
        int k = 0;
        Range range = new Range();
        if (text.length() == mask.length()) {
            MaskedEditText.this.removeTextChangedListener(MaskedEditText.this);
            cleanUp();

            for (int i = 0; i < text.length(); ++i) {
                if (mask.charAt(i) == charRepresentation) {
                    range.setStart(k);
                    range.setEnd(k);
                    rawText.subtractFromString(range);
                    rawText.addToString(String.valueOf(text.charAt(i)), k, maxRawLength);
                    setText(makeMaskedText());
                    k++;
                }
            }
            setText(makeMaskedText());

            post(new Runnable() {
                @Override
                public void run() {
                    MaskedEditText.this.addTextChangedListener(MaskedEditText.this);
                }
            });
        }
    }

It perfectly works with screen rotation (tested with mask "+1 (###) ###-####" and number +1 (908) 169-2608), but after rotate cursor position jumping to the end...not so necessarily,but a little buggy

Neznakomec avatar Apr 29 '16 21:04 Neznakomec