Format rut in TextView

2

I am working on an app which contains a form of Login (rut and pass), they ask me that when entering values in the field of rut it will deliver the format instantly, for example: xx.xxx.xxx-x .

You can give me some suggestions on how to do it.

I update my question.

in the onCreate of the Login class, I call the MaskWatcher class,

login.addTextChangedListener(new MaskWatcher(login.toString()));

In this class I perform a validation to call a function that gives me the formatted rut.

@Override
public void afterTextChanged(Editable editable) {
    Log.i("MaskWatcher","Valor Editable"+editable);


    String valortext = editable.toString();
    Log.i("MaskWatcher","Valor valortext"+valortext);
    if(valortext.equals(""))
    {
        Log.i("MaskWatcher","Valor valortext 0"+valortext);
        valortext = "0";
    }
    else
    {
        Log.i("MaskWatcher","Valor valortext !0"+valortext);
        valortext = FormatearRUT(valortext);
    }
}
public  String FormatearRUT(String rut) {

    Log.i("MaskWatcher","FormatearRUT valor"+rut);
    int cont = 0;
    String format;
    rut = rut.replace(".", "");
    rut = rut.replace("-", "");
    format = "-" + rut.substring(rut.length() - 1);
    for (int i = rut.length() - 2; i >= 0; i--) {
        format = rut.substring(i, i + 1) + format;
        cont++;
        if (cont == 3 && i != 0) {
            format = "." + format;
            cont = 0;
        }
    }
    Log.i("MaskWatcher","FormatearRUT valorformat"+format);


    return format;
}

and it is here where it complicates me a bit, because I need to print the format value in the edittext that is in the other class, the Login class. How could I do this?

    
asked by Rodrigo 19.03.2018 в 20:24
source

3 answers

0

I do not understand very well what xx.xxx.xxx-x format means but a solution to format the text input in an edittext could be:

 tueditext = (EditText)findViewById(R.id.tueditext);
 tueditext.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

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

        }

        @Override
        public void afterTextChanged(Editable s) {
            String valortext = tueditext.getText().toString().trim();
            if(valortext.equals(""))
                {
                valortext = "0";
                }
            Formatear tu valortext con el formato que quieras.
            }
    });

I hope it serves you.

    
answered by 19.03.2018 в 22:07
0

You can use this function that formats the entered routing to the format you need:

    public String FormatearRUT(String rut) {
            int cont = 0;
            String format;
            rut = rut.replace(".", "");
            rut = rut.replace("-", "");
            format = "-" + rut.substring(rut.length() - 1);
            for (int i = rut.length() - 2; i >= 0; i--) {
                format = rut.substring(i, i + 1) + format;
                cont++;
                if (cont == 3 && i != 0) {
                    format = "." + format;
                    cont = 0;
                }
            }
            return format;
    }
   //Tomado de la primera respuesta 
    tueditext = (EditText)findViewById(R.id.tueditext);
    tueditext.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

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

                }

                @Override
                public void afterTextChanged(Editable s) {
                    String valortext = tueditext.getText().toString().trim();
                    if(valortext.equals(""))
                        {
                        valortext = "0";
                        }
                    else
                    {
                      valortext = FormatearRUT(valortext); //Sustituyes por la funcion que te formateara el rut
                    }
                }
            });
    
answered by 19.03.2018 в 22:25
0

If the rut is a numeric you could use DecimalFormat

Example

editRut = (EditText)findViewById(R.id.editRut);
 editRut.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

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

        }

        @Override
        public void afterTextChanged(Editable s) {
            String valortext = tueditext.getText().toString().trim();
            if(valortext.equals(""))
                {
                valortext = "0";
                }
           DecimalFormat formater = new DecimalFormat("xx.xxx.xxx-x");
           editRut.setText(formater.format(Double.parseDouble(s)));
            }
    });
    
answered by 20.03.2018 в 16:42