Problems with field mask

1

My application is closing when I make a few clicks in the field, I noticed that it is trying to double-mask the field.

@FocusChange(R.id.edtTelefone)
void focusAlterado(View v, boolean hasFocus) {
    String telefone = edtTelefone.getText().toString();
    MaskEditTextChangedListener maskTEL = new MaskEditTextChangedListener("(##) #### ####", edtTelefone);
    if(!hasFocus) {
        if(telefone.length() != 14) {
            edtTelefone.addTextChangedListener(maskTEL);
            a++;
        }
    }else{
        if(telefone.length() == 0){
            edtTelefone.addTextChangedListener(maskTEL);
            a++;
        }else if(a != 1){
            edtTelefone.addTextChangedListener(maskTEL);
            a++;
        }
    }
}

I tried to do this trick of a but it did not work.

    
asked by anonymous 29.05.2017 / 21:23

1 answer

2

The mask needs to be added only once, so you should not try to add each time the view changes focus. You need to add the mask through the onCreate (Bundle), in the case of an Activity, or the onCreateView (LayoutInflater, ViewGroup, Bundle), in the case of a Fragment. As the example below shows:

MaskEditTextChangedListener maskTEL = new MaskEditTextChangedListener("(##) #### ####", edtTelefone);
edtTelefone.addTextChangedListener(maskTEL);
    
10.06.2017 / 19:39