Android - 处理虚拟&物理键盘事件
在阅读了几个类似问题的答案*之后,我确实意识到 onKeyListener() 不会从软(虚拟)键盘获取按键事件。它只能从硬(物理)键盘获取它们。解决方法是使用 TextWatcher 或 onKeyboardActionListener。我有以下问题:
(1)有没有一种方法能够通过仅实现一个侦听器来侦听任何键盘(软键盘或硬键盘)的按键操作?或者基本上是一个同时适用于两者的 API?
(2) TextWatcher 或 onKeyboardActionListener 与 onKeyListener() 的 onKey() 方法不同,不会传递当前具有焦点的视图(并且用户正在其中键入输入)。那么,如果我要使用 TextWatcher 或 onKeyboardActionListener,如何获取当前聚焦的视图?我需要它能够根据输入在用户键入其输入的 EditText 上设置一些属性。
*相关问题: onKeyListener 无法在虚拟键盘上工作, onKeyListener 无法使用软键盘 (Android), Android:为什么我的 OnKeyListener() 没有被调用?
谢谢!
After reading answers to several similar questions* I did realize that onKeyListener() does not get key press events from a soft (virtual) keyboard. It gets them only from a hard (physical) keyboard. And the workaround would be either to use a TextWatcher or onKeyboardActionListener. I have following questions:
(1) Is there a way to be able to listen to key presses from any keyboard (soft or hard) by just implementing one listener? or basically a single API that works for both?
(2) TextWatcher or onKeyboardActionListener, unlike onKeyListener()'s onKey() method, do not pass the view that currently has focus (and in which the user is typing input). So, how do I get the currently focussed view if I were to use TextWatcher or onKeyboardActionListener? I need this to be able to set some properties on the EditText in which the user is keying their input, based on the input.
*Related questions:
onKeyListener not working on virtual keyboard,
onKeyListener not working with soft keyboard (Android),
Android: why is my OnKeyListener() not called?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我也有同样的问题。假设没有好的方法来实现一种处理软键盘事件的解决方案。我已经为
delete
事件实现了onKeyListener()
,为按键事件实现了 TextWatcher 。TextWatcher 方法有一个很大的缺点 - 您无法更改链接的 EditText 的可编辑内容 - 它将导致循环。当心!
I've got the same problem. And suppose, that there is no good way to implement one solution for handling soft keyboard events. I've implemented
onKeyListener()
fordelete
event and TextWatcher for keys event.There is one big disadvantage with TextWatcher approach - you cannot change editable that your EditText is linked - it will cause the loop. Be careful!