How do I use InputFilter to limit characters in an EditText in Android?

后端 未结 20 1134
慢半拍i
慢半拍i 2020-11-22 04:23

I want to restrict the chars to 0-9, a-z, A-Z and spacebar only. Setting inputtype I can limit to digits but I cannot figure out the ways of Inputfilter looking through the

20条回答
  •  情歌与酒
    2020-11-22 05:01

    If you subclass InputFilter you can create your own InputFilter that would filter out any non-alpha-numeric characters.

    The InputFilter Interface has one method, filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend), and it provides you with all the information you need to know about which characters were entered into the EditText it is assigned to.

    Once you have created your own InputFilter, you can assign it to the EditText by calling setFilters(...).

    http://developer.android.com/reference/android/text/InputFilter.html#filter(java.lang.CharSequence, int, int, android.text.Spanned, int, int)

提交回复
热议问题