I need to set a number of decimal places for EditText
on Android. For this I used InputFilter
as I show 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 enter a value such as 22.85
and click the button next to the keyboard, the field loses the decimal places. If I tell 22.80
it works normally.