CPF / Cnpj Mask in Edittext [duplicate]

3

I have an Edittext field, I want to know how I can set a mask for it to format the text entered in the corresponding format, CPF or CNPJ, I tried using the method described in this one question however when the text entered reaches 11 digits to define it as cpf, all digits are changed by 1 with eg

  

111.111.111-11

I searched but could not find where I went wrong, and it will be possible when the first three digits are entered, one. already be inserted?

My code:

public class CpfCnpjMaks {

    private static final String cpfMask = "###.###.###-##";
    private static final String cnpjMask = "##.###.###/####-##";

    public static String unmask(String s){

        return s.replaceAll("[^0-9]*", "");

    }

    public static TextWatcher insert(final EditText editText){

        return new TextWatcher() {

            boolean isUpdating;
            String old = "";

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

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                String str = CpfCnpjMaks.unmask(s.toString());
                String mask = "";
                String defaultMaks = getDefaultMask(str);
                switch (str.length()){

                    case 11:
                        mask = cpfMask;
                        break;
                    case 14:
                        mask = cnpjMask;
                        break;
                }

                String mascara = "";
                if (isUpdating){

                    old = str;
                    isUpdating = false;
                    return;
                }

                int i = 0;
                for (char m : mask.toCharArray()){

                    if ((m != '#' && str.length()> old.length()) ||(m != '#' && str.length() <old.length()) && str.length() != i){

                       mascara += m;
                       continue;

                    }

                    try {
                        mascara += str.charAt(i);
                    }catch (Exception e){

                        break;

                    }

                    isUpdating = true;
                    editText.setText(mascara);
                    editText.setSelection(mascara.length());

                }

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        };

    }

    private static String getDefaultMask(String str){

        String defaultMaks = cpfMask;
        if (str.length()>11){

            defaultMaks = cnpjMask;

        }

        return defaultMaks;
    }

}
    
asked by anonymous 15.01.2016 / 20:04

1 answer

9

A default is missing on your switch, so try:

public abstract class CpfCnpjMaks {

    private static final String maskCNPJ = "##.###.###/####-##";
    private static final String maskCPF = "###.###.###-##";


    public static String unmask(String s) {
        return s.replaceAll("[^0-9]*", "");
    }

    public static TextWatcher insert(final EditText editText) {
        return new TextWatcher() {
            boolean isUpdating;
            String old = "";

            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String str = CpfCnpjMaks.unmask(s.toString());
                String mask;
                String defaultMask = getDefaultMask(str);
                switch (str.length()) {
                    case 11:
                        mask = maskCPF;
                        break;
                    case 14:
                        mask = maskCNPJ;
                        break;

                    default:
                        mask = defaultMask;
                        break;
                }

                String mascara = "";
                if (isUpdating) {
                    old = str;
                    isUpdating = false;
                    return;
                }
                int i = 0;
                for (char m : mask.toCharArray()) {
                    if ((m != '#' && str.length() > old.length()) || (m != '#' && str.length() < old.length() && str.length() != i)) {
                        mascara += m;
                        continue;
                    }

                    try {
                        mascara += str.charAt(i);
                    } catch (Exception e) {
                        break;
                    }
                    i++;
                }
                isUpdating = true;
                editText.setText(mascara);
                editText.setSelection(mascara.length());
            }

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

            public void afterTextChanged(Editable s) {
            }
        };
    }

    private static String getDefaultMask(String str) {
        String defaultMask = maskCPF;
        if (str.length() > 11){
            defaultMask = maskCNPJ;
        }
        return defaultMask;
    }
}
    
15.01.2016 / 21:04