Edittext is deleted when use setSelection() within onTextChanged method

Asked

Viewed 132 times

2

I’m trying to put a phone mask on by the method onTextChanged, the text received in this method is coming in reverse, and when I try to put the method setSelection() for the cursor to position at the end, my EditText is deleted in Galaxy Tab 10. How to solve this?

private TextWatcher filterTextWatcherTelefone = new TextWatcher() {
        public void afterTextChanged(Editable s) {


        }

        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
        }

        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            try {
                if (atualizando) {
                    atualizando = false;
                    return;
                }


                String resultado = limparFormatacaoNumero(s.toString());

                if (isNumero(resultado)) {

                    if (resultado.length() <= 14) {
                        resultado = adicionarFormatacaoTelefone(resultado);

                    } else {
                        resultado = resultado.substring(0, 14);
                        resultado = adicionarFormatacaoTelefone(resultado);
                    }
                    atualizando = true;
                    textoDiscagem.setText(resultado);
                    textoDiscagem.setSelection(textoDiscagem.getText().length());

                }
            } catch (Exception e) {
            }
        }
    };
  • Have you even tested the PhoneNumberFormattingTextWatcher? Check the documentation to see how to configure it: http://developer.android.com/reference/android/telephony/PhoneNumberFormattingTextWatcher.html.

2 answers

1

A great solution would be the implementation of an already Android class, the PhoneNumberFormattingTextWatcher:

PhoneNumberFormattingTextWatcher mPhoneWatcher = new PhoneNumberFormattingTextWatcher();       
etPhone.addTextChangedListener(mPhoneWatcher);

Just one detail about this class

It uses as standard the Locale standard, that is, if the user’s device is in English, the formatted number will be in English.

In the above documentation, you are saying that you accept the constructor PhoneNumberFormattingTextWatcher(String countryCode), however, if we open the javadoc this constructor has a tag @hide, that is, is not available in the SDK to use.

0

I solved the problem by putting in Edittext via xml a tag:

android:inputType="textVisiblePassword"

found that the problem was in auto-complete.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.