K
K
knott2013-11-29 18:09:22
Android
knott, 2013-11-29 18:09:22

Text transformation in EditText?

Trying to do text formatting while writing it in android.
For example phone number:
I enter in EditText: +79032285588
And I see: +7 (903) 227-55-88
I tried to solve the problem using TransformationMethod , which is used, for example, to enter passwords (each input character is changed to an asterisk)

public class PhoneNumberTransformationMethod implements TransformationMethod {

    public static final String TAG = "PhoneNumberTransformationMethod";


    @Override
    public CharSequence getTransformation(CharSequence source, View view) {
        // Такой странный код используется потому, что source — mutable.
        if (source instanceof Spanned) {
            Spanned spanned = (Spanned) source;
            return new PhoneNumberCharSequence(source);
        } else {
            return PhoneUtils.formatPhoneNumber(source.toString());
        }
    }

    @Override
    public void onFocusChanged(View view, CharSequence charSequence, boolean b, int i, Rect rect) {
    }

    private static class PhoneNumberCharSequence implements CharSequence {

        private HashMap<String, String> phoneNumbers = new HashMap<String, String>();
        private CharSequence mSource;

        public PhoneNumberCharSequence(CharSequence source) {
            mSource = source;
        }

        private String getCurrent() {
            String currentString = mSource.toString();

            String result = phoneNumbers.get(currentString);
            if (result == null) {
                result = PhoneUtils.formatPhoneNumber(currentString);
                phoneNumbers.put(currentString, result);
            }

            return result;
        }

        @Override
        public int length() {
            return getCurrent().length();
        }

        @Override
        public char charAt(int i) {
            return getCurrent().charAt(i);
        }

        @Override
        public CharSequence subSequence(int start, int end) {
            return new SpannedString(this).subSequence(start, end);
        }
    }
}

The problem is that this doesn't work for cases where input characters are not just changed, but new ones are added. When you enter the next character, CharSequenceonly one character is taken from the returned character, even if the telephone number formatter returned several.
Is it possible to make the TextView(EditText) reread the entire line from my CharSequence?
Or maybe there are some other ways to implement the above described task?
Thank you very much in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
itspers, 2013-11-29
@knott

Beret or ready watcher

EditText inputField = (EditText) findViewById(R.id.inputfield);
inputField.addTextChangedListener(new PhoneNumberFormattingTextWatcher());

or just TextWatcher and format it how you want stackoverflow.com/questions/15494093/phone-number-format-in-android

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question