onKeyListener 不适用于软键盘 (Android)
我正在使用 onKeyListener 来获取 onKey 事件。它可以与普通键盘配合使用。但它不适用于软键盘。我只能获取数字的 onKey 事件,而不能获取字母的 onKey 事件。有什么解决方法可以解决这个问题吗?任何形式的帮助将不胜感激。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我不相信 OnKeyListener 会被软件键盘调用。这与软件键盘是 IME 设备以及 IME 设备可能是键盘以外的设备有关。但这似乎使 onKeyListener 几乎毫无用处,因为它仅适用于带有硬件键盘的手机。我最近通过在我的 EditText 字段上使用 TextWatcher 解决了这个问题Activity 而不是使用 OnKeyListener。
I don't believe an OnKeyListener gets called at all with the software keyboard. It has something to do with the software keyboard being an IME device and IME devices possibly being things other than keyboards. It seems to make onKeyListener pretty much useless though, since it only works on phones with hardware keyboards. I worked around this issue recently by using TextWatcher on the EditText field in my Activity instead of using OnKeyListener.
onKeyListener 通过软键盘在 Android 1.5 上完美工作
从 Android 1.6 开始,字符和数字键不再通过 onKey 事件,但 DEL 键却会
令人沮丧
onKeyListener worked perfectly on Android 1.5 via the soft keyboard
From Android 1.6 onwards the character and number keys are not going via the onKey event, yet the DEL key does
Frustrating
这可能很愚蠢,但这就是 Android 目前的工作方式。
该文档指出,按键事件将仅针对硬件击键传播,而不是针对软件击键。
实际上,不鼓励设备制造商通过按键侦听器传播软键盘事件,尽管完全取决于制造商是否尊重这一点或实际上平等对待软键盘和硬键盘。
从Android 4.2.2开始,Android系统本身就不再支持软键盘的按键事件,所以即使是厂商也无法选择自己的方式。
因此,这里唯一万无一失的选择是实现您自己的 IME(软键盘),并自己处理击键。
TextWatcher 主要用于替换关键侦听器,但是 editText.setText(...);还将触发 TextWatcher 事件,因此如果仅对键入的按键感兴趣,那么 TextWatcher 可能也不是一种解决方案。
将 TextWatcher 与 AutocomleteTextView 或 EditText 一起使用时请小心。不要在 TextWatcher 事件中修改 AutocompleteTextView / EditText 内容中的文本,否则您很可能会陷入无限事件/侦听循环。
希望这有助于澄清可用的选项,但遗憾的是它没有提供有效的解决方案。
令人失望的是,谷歌错过了其用户界面的这一重要方面。
This is probably stupid, but that's how Android works at the moment.
The documentation states that the key events will only be propagated for the hardware key strokes, not software.
The device manufacturers are actually being discouraged to propagate soft keyboard events through key listeners, although it is completely up to the manufacturer to honour that or to actually treat the soft and hard keyboards with equal terms.
Starting from Android 4.2.2, Android system itself will not support key stoke events for the soft keyboards at all, so even the manufacturers will not be able to choose their way.
So the only foolproof option here is to implement your own IME (soft keyboard), and handle the keystrokes yourself.
TextWatcher can be used mostly to replace the key listeners, however editText.setText(...); will also trigger the TextWatcher events, so if one is interested in typed keys only then probably TextWatcher is not a solution either.
Please be cautious when using TextWatcher with AutocomleteTextView or EditText. Do not modify text in the AutocompleteTextView / EditText's content from within TextWatcher events, cause otherwise you'll most probably end up in an infinite event/listening loop.
Hope this helps to clarify the available options, but sadly it does not provide a working solution.
Disappointing that Google has missed on this important aspect of their UI.
这似乎是特定于设备的。我可以确认这适用于 Xoom 和 Acer A100。但是,Samsung Galaxy Tab Plus 仅触发非字符按钮的事件。 (所有运行 Honeycomb 的设备)
This seems to be device specific. I can confirm that this works on the Xoom and the Acer A100. However, the Samsung Galaxy Tab Plus only fires the event for the non-character buttons. (All devices running Honeycomb)
我通过将侦听器放入它自己的方法中并在第一次后再次调用它来解决这个问题。在 onCreate 中我调用 setKeyListenerForEnter();
然后,这是方法:
public void setKeyListenerForEnter(){
我不确定这是否是比处理 IME 键盘本身更好的解决方案,但它是一个解决方案。
I got around this by putting the listener into it's own method and calling it again after the first time. In the onCreate I call setKeyListenerForEnter();
Then, here's the method:
public void setKeyListenerForEnter(){
I'm not sure if this is a better solution than handling the IME keyboard itself, but it is a solution.
例子:
example: