-1

I get this code when I call

EditText.filters = arrayOf<InputFilter>(InputFilter.LengthFilter(7),DecimalDigitsInputFilter(2))

Any solution?

GrandPC
  • 31
  • 6

1 Answers1

0

Try this:

 class DecimalDigitsInputFilter( maxDecimalPlaces: Int) : InputFilter {
            private val pattern: Pattern = Pattern.compile(
                "[0-9]"  + "+((\\.[0-9]{0,"
                        + (maxDecimalPlaces - 1) + "})?)||(\\.)?"
            )
    
            override fun filter(
                p0: CharSequence?,p1: Int,p2: Int,p3: Spanned?,p4: Int,p5: Int
            ): CharSequence? {
                p3?.apply {
                    val matcher: Matcher = pattern.matcher(p3)
                    return if (!matcher.matches()) "" else null
                }
                return null
            }
        }

Implementation in activity

  etContactUsName.filters = arrayOf<InputFilter>(DecimalDigitsInputFilter(maxDigitsAfterDot))
Android Geek
  • 8,956
  • 2
  • 21
  • 35
  • Thanks. Did you convert code from [here](https://stackoverflow.com/questions/17423483/how-to-limit-edittext-length-to-7-integers-and-2-decimal-places)? For me optimizing imports and formatting code forever (5 mins) when I tried to convert. – GrandPC Jul 13 '21 at 05:20