androidkotlinandroid-input-filter

Unresolved reference: DecimalDigitsInputFilter


I get this code when I call

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

Any solution?


Solution

  • 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))