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 docs.
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
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)
to prevent words in edittext. create a class that u could use anytime.
To avoid Special Characters in input type
You can set filter to your edit text like below
Use this its work 100% your need and very simple.
In strings.xml
This is an old thread, but the purposed solutions all have issues (depending on device / Android version / Keyboard).
DIFFERENT APPROACH
So eventually I went with a different approach, instead of using the
InputFilter
problematic implementation, I am usingTextWatcher
and theTextChangedListener
of theEditText
.FULL CODE (EXAMPLE)
The reason
InputFilter
is not a good solution in Android is since it depends on the keyboard implementation. The Keyboard input is being filtered before the input is passed to theEditText
. But, because some keyboards have different implementations for theInputFilter.filter()
invocation, this is problematic.On the other hand
TextWatcher
does not care about the keyboard implementation, it allows us to create a simple solution and be sure it will work on all devices.It is possible to use
setOnKeyListener
. In this method, we can customize the inputedittext
!