Setting up masking field android phone

0

I'm trying to automatically set a mask on the field, I do not know any other way, I'm doing this:

if(s.equals('(')){
    edtCelular.setText(texto);
}else{
    texto = "(" + telefone.substring(0, 2) + ") " + telefone.substring(2, 7) + "-" + telefone.substring(7, 11);
    edtCelular.setText(texto);
}

And it's not working, is there any way to do this?

It is being set when the field focus is coming out. As an indication of a response I did the upgrade but still give some different problems.

I have class MASK that looks like this:

package com.fomedemais.FomeDemais;

 import android.text.Editable;
 import android.text.TextWatcher;
 import android.widget.EditText;

public class Mask {

public static String unmask(String s) {
    return s.replaceAll("[.]", "").replaceAll("[-]", "")
            .replaceAll("[/]", "").replaceAll("[(]", "")
            .replaceAll("[)]", "");
}

public static TextWatcher insert(final String mask, final EditText ediTxt) {
    return new TextWatcher() {
        boolean isUpdating;
        String old = "";
        public void onTextChanged(CharSequence s, int start, int before,int count) {
            String str = Mask.unmask(s.toString());
            String mascara = "";
            if (isUpdating) {
                old = str;
                isUpdating = false;
                return;
            }
            int i = 0;
            for (char m : mask.toCharArray()) {
                if (m != '#' && str.length() > old.length()) {
                    mascara += m;
                    continue;
                }
                try {
                    mascara += str.charAt(i);
                } catch (Exception e) {
                    break;
                }
                i++;
            }
            isUpdating = true;
            ediTxt.setText(mascara);
            ediTxt.setSelection(mascara.length());
        }
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
        public void afterTextChanged(Editable s) {}
    };
 }

}

And when I do the event I'm doing this:

@FocusChange(R.id.edtTelefone)
void focusAlterado(View v, boolean hasFocus) {
    if(!hasFocus){
        edtTelefone.addTextChangedListener(Mask.insert("(##)####-####", edtTelefone));
    }
}

LOG ERROR

  • WHEN I SAY FOR THE FIRST TIME IT IS NOT AUTOMATICALLY GETTING THE MASK
  • DEPENDING ON HOW I MAKE IT LOCK THE APPLICATION AND LOCK, WITHOUT ANY ERROR LOG. THIS HAPPENS WHEN I GO ON THE DIGITAL PHONE, THEN I CLICK ON THE DIGITAL CELL PHONE, I CALL OUT AND DIGIT WHEN I WILL DELETE THE PHONE I HOLD
  • asked by anonymous 15.05.2017 / 19:35

    2 answers

    1

    Talk Renan,

    It's much simpler than you think, look at this example:

    id_do_campo.addTextChangedListener(Mask.insert("(##)####-####", id_do_campo));
    

    Done, the mask is done.

    ------------ Edit:

    Here is the code for the Mask class:

    import android.text.Editable;
    import android.text.TextWatcher;
    import android.widget.EditText;
    
    public class Mask {
    
        public static String unmask(String s) {
            return s.replaceAll("[.]", "").replaceAll("[-]", "")
                    .replaceAll("[/]", "").replaceAll("[(]", "")
                    .replaceAll("[)]", "");
        }
    
        public static TextWatcher insert(final String mask, final EditText ediTxt) {
            return new TextWatcher() {
                boolean isUpdating;
                String old = "";
                public void onTextChanged(CharSequence s, int start, int before,int count) {
                    String str = Mask.unmask(s.toString());
                    String mascara = "";
                    if (isUpdating) {
                        old = str;
                        isUpdating = false;
                        return;
                    }
                    int i = 0;
                    for (char m : mask.toCharArray()) {
                        if (m != '#' && str.length() > old.length()) {
                            mascara += m;
                            continue;
                        }
                        try {
                            mascara += str.charAt(i);
                        } catch (Exception e) {
                            break;
                        }
                        i++;
                    }
                    isUpdating = true;
                    ediTxt.setText(mascara);
                    ediTxt.setSelection(mascara.length());
                }
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
                public void afterTextChanged(Editable s) {}
            };
        }
    
    }
    
        
    15.05.2017 / 19:50
    0

    I created a Kotlin extension of TextWatcher that solves this problem. It works for numbers in both Brazilian formats: (XX) XXXX XXXX and (XX) XXXXX XXXX.

    Follow the link: link

    Example usage:

        val editText: EditText() // The field you want to be formatted
        val country = PhoneNumberFormatType.EN_US // OR PhoneNumberFormatType.PT_BR
        val phoneFormatter = PhoneNumberFormatter(WeakReference(editText), country)
        editText.addTextChangedListener(phoneFormatter)
    
        
    20.04.2018 / 19:38