0
I would like to know how to create a mask in an Edittext in ABC-1234 format. I’m trying something as an example below, but still unsuccessful.
public abstract class PlacaVeiculoMask {
public static String PLACA_MASK = "???-####";
public static String unmask(String s) {
return s.replaceAll("[A-Z]{3}-[0-9]{4}", "");
}
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 = PlacaVeiculoMask.unmask(s.toString());
String mask = PLACA_MASK;
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) {
}
};
}
}
Here with me it didn’t work. The only thing I changed was the language, I did this example of code in Java
– Filipe Amaral Neis
what exactly did not work, the mask did not reach the edittext?
– LMaker
My edittext recognizes the mask, enters the function. However it does not format in the correct value.
– Filipe Amaral Neis