What is the best way to apply a mask to EditText on Android?


what's the best way to mask EditText on Android?
I would like my EditText to behave the way this decimal input number is here . Is there an easy way to do this?

+7
java android
source share
3 answers

You need to programmatically set the InputFilter to EditText to setFilters .

From the documentation:

InputFilters can be tied to Editables to limit the changes that can be made to them.

You can even change the user input, for example by adding the decimal point you want if I get you right.

+2
source share

I built a decimal mask for the editing text, which will automatically change the editing text to the number of decimal places needed. In bass mode, you listen to text changes and loss of focus.

  private void formatNumber() { sNumberToFormat = etNumberToFormat.getText().toString(); sDecimalMask = etDecimalMask.getText().toString(); boolean periodMask = false; String delimiter = getDelimiter(); String[] decimalMask = getsDecimalMask(); if (decimalMask.length == 1) { return; } else { if (delimiter.equalsIgnoreCase(",")) { //decimal format only currently works with dot delimiters. sDecimalMask = sDecimalMask.replace(",", "."); } DecimalFormat df = new DecimalFormat(sDecimalMask); df.setRoundingMode(RoundingMode.UP); sNumberToFormat = df.format(Float.valueOf(sNumberToFormat.replace(",", "."))); //if (maxNumber > Float.valueOf(sNumberToFormat)) { if (delimiter.equalsIgnoreCase(",")) { sNumberToFormat = sNumberToFormat.replace(".", ","); } etNumberToFormat.setText(sNumberToFormat); } } 

Full demo here .

0
source share

I think you can use:

 android:numeric="decimal" 

on your edittext

-one
source share

All Articles