0
I need to set a number of decimals for a EditText
on Android. For this I used a InputFilter
as shown below:
public NumeroFormatado(int digitsBeforeZero,int digitsAfterZero)
{
mPattern=Pattern.compile("[0-9]{0," + (digitsBeforeZero - 1) + "}+((\\.[0-9]{0," + (digitsAfterZero - 1) + "})?)||(\\.)?");
}
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
Matcher matcher=mPattern.matcher(dest);
if(!matcher.matches())
return "";
return null;
}
And in my Activity I define:
editValor = (EditText) findViewById(R.id.editValor);
editValor.setFilters(new InputFilter[] {new NumeroFormatado(10,2)});
But when I report a value as for example 22.85
and click on the button next to the keyboard, the field loses the decimals. If I inform 22.80
works normally.
Thank you there for your reply. It was exactly what I was looking for!
– Gustavomgu.developer