Problems with masks in the field

Asked

Viewed 68 times

1

My application is closing when I do a few clicks in the field, I found that it is trying to add twice mascara to 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 didn’t work.

1 answer

2


The mask needs to be added only once, so you should not try to add every time the view shifts focus. You need to add the mask via onCreate(Bundle), in the case of an Activity, or onCreateView(Layoutinflater, Viewgroup, Bundle) in the case of a Fragment. As shown in the example below:

MaskEditTextChangedListener maskTEL = new MaskEditTextChangedListener("(##) #### ####", edtTelefone);
edtTelefone.addTextChangedListener(maskTEL);

Browser other questions tagged

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