Android - 处理虚拟&物理键盘事件

发布于 2024-10-26 06:55:10 字数 860 浏览 3 评论 0原文

在阅读了几个类似问题的答案*之后,我确实意识到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无人接听 2024-11-02 06:55:22

我也有同样的问题。假设没有好的方法来实现一种处理软键盘事件的解决方案。我已经为 delete 事件实现了 onKeyListener() ,为按键事件实现了 TextWatcher 。

m_edtRecipients.addTextChangedListener(new TextWatcher() {
        boolean bConsumed = false;

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (!bConsumed) {
                RecipientsTextStyle.format(m_edtRecipients.getText(), m_dbProcessor);
            }
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            if (count != 0) {
                bConsumed = true;
                Log.d(TAG, "delete true");
            } else {
                bConsumed = false;
                Log.d(TAG, "erase false");
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

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() for delete event and TextWatcher for keys event.

m_edtRecipients.addTextChangedListener(new TextWatcher() {
        boolean bConsumed = false;

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (!bConsumed) {
                RecipientsTextStyle.format(m_edtRecipients.getText(), m_dbProcessor);
            }
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            if (count != 0) {
                bConsumed = true;
                Log.d(TAG, "delete true");
            } else {
                bConsumed = false;
                Log.d(TAG, "erase false");
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

There is one big disadvantage with TextWatcher approach - you cannot change editable that your EditText is linked - it will cause the loop. Be careful!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文