如何在android EditText中强制使用英文键盘
我知道如何强制数字、文本等...,但是是否有一个标志或 IME 选项让我强制 EditText 仅接受某些语言(如英语),以防我的数据库字段只能接受英文字符?当然,我可以检查并通知用户错误的输入,但这并不完全是用户友好的......
在 EditText 上实现我自己的过滤器也可能是可能的,但我不确定它会强制键盘布局本身位于我需要的语言。
有什么想法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用
flagForceAscii
或EditorInfo.IME_FLAG_FORCE_ASCII
。该标志位于 IME 选项上。所以在 XML 中你可以把这个属性放在 EditText 中,<代码>
imeOptions="flagForceAscii"
当然,这只会影响键盘的初始状态。
https://developer.android.com/reference/android/view /inputmethod/EditorInfo.html#IME_FLAG_FORCE_ASCII
You can use
flagForceAscii
orEditorInfo.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:
With symbols that's, you have defines in digits string:
可能的破解方法是使用 EditText 的
inputType
,例如textVisiblePassword
。这迫使键盘仅支持英文字符和数字,但缺点是它还禁用了键盘上的麦克风。
无论如何,这可以满足您的要求。
The possible hack to this could be using EditText's
inputType
liketextVisiblePassword
.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.