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?