I wrote a form to check the region by phone number. Put the TextWatcher on the input form, it tracks the value of the input field and substitutes the mask, that's only if I delete the value and try to enter the number of another region, then another mask is substituted and an error occurs
java.lang.StackOverflowError at android.text.method.ReplacementTransformationMethod $ ReplacementCharSequence.getChars (ReplacementTransformationMethod.java:151)
It happens as I understand it because I try to assign two masks to EditText, the question is how do I delete the previous mask and replace it with a new one?
editPhone.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { if(editPhone.getText().toString().equals("+3")){ editPhone.addTextChangedListener(new MaskedWatcher("+38 (###) ###-##-##")); } if(editPhone.getText().toString().equals("+7")){ editPhone.addTextChangedListener(new MaskedWatcher("+7 (###) ###-##-##")); } if(editPhone.getText().toString().equals("")){ editPhone.setText("+"); editPhone.setSelection(editPhone.getText().length()); } if(editPhone.getText().toString().equals("+1")){ editPhone.addTextChangedListener(new MaskedWatcher("+1 (###) ###-##-##")); } } @Override public void afterTextChanged(Editable editable) { } });