Is there a way to define a min and max value for EditText in Android?

后端 未结 25 1250
你的背包
你的背包 2020-11-22 11:51

I want to define a min and max value for an EditText.

For example: if any person tries to enter a month value in it, the value must be between 1-12.

相关标签:
25条回答
  • 2020-11-22 12:48

    Very simple example on Kotlin:

    import android.text.InputFilter
    import android.text.Spanned
    
    class InputFilterRange(private var range: IntRange) : InputFilter {
    
        override fun filter(source: CharSequence, start: Int, end: Int, dest: Spanned, dstart: Int, dend: Int) = try {
            val input = Integer.parseInt(dest.toString() + source.toString())
            if (range.contains(input)) null else ""
        } catch (nfe: NumberFormatException) {
            ""
        }
    }
    
    0 讨论(0)
提交回复
热议问题