I know how to force numbers, texts etc.., but is there a flag or IME options for me to force the EditText to accept only certain language like English in case my DB fields can accept only English characters? of course I can check and notify the users on bad input but that's开发者_开发问答 not exactly user friendly....
Implementing my own filter on the EditText might also be possible but I'm not sure it will force the keyboard layout itself to be in the language I need.
Any idea?
You can use flagForceAscii
or EditorInfo.IME_FLAG_FORCE_ASCII
. The flag is on the IME options. So in XML you can put this attribute in the EditText,
imeOptions="flagForceAscii"
This just affects the initial state of the keyboard, of course. https://developer.android.com/reference/android/view/inputmethod/EditorInfo.html#IME_FLAG_FORCE_ASCII
For English Character only:
android:digits="abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ"
With symbols that's, you have defines in digits string:
android:digits="abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ !@#$%^*?"
The possible hack to this could be using EditText's inputType
like textVisiblePassword
.
This forces the keyboard to support only English Characters and Numbers , but on the downside it also disabled mic on the keyboard.
Anyways this could work for your requirement.
You should probably filter the input anyways. Forcing English keyboard doesn't force user to use only English letters. User can still long press a character or have a non-english hardware keyboard.
You could listen to text change events and reject all characters that are not accepted by your DB at that point.
精彩评论