简体   繁体   English

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

[英]Android - handle virtual & physical keyboard events

After reading answers to several similar questions* I did realize that onKeyListener() does not get key press events from a soft (virtual) keyboard. 在阅读了几个类似问题的答案之后*我确实意识到onKeyListener()没有从软(虚拟)键盘获得按键事件。 It gets them only from a hard (physical) keyboard. 它只能从硬(物理)键盘获取它们。 And the workaround would be either to use a TextWatcher or onKeyboardActionListener. 解决方法是使用TextWatcher或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? (1)有没有办法能够通过实现一个监听器从任何键盘(软或硬)听按键? or basically a single API that works for both? 或基本上是一个适用于两者的API?

(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). (2)TextWatcher或onKeyboardActionListener与onKeyListener()的onKey()方法不同,不传递当前具有焦点的视图(用户正在键入输入)。 So, how do I get the currently focussed view if I were to use TextWatcher or onKeyboardActionListener? 那么,如果我要使用TextWatcher或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. 我需要这个能够在EditText上设置一些属性,用户根据输入键入他们的输入。

*Related questions: onKeyListener not working on virtual keyboard , onKeyListener not working with soft keyboard (Android) , Android: why is my OnKeyListener() not called? *相关问题: onKeyListener无法使用虚拟键盘onKeyListener无法使用软键盘(Android)Android:为什么我的OnKeyListener()没有被调用?

Thanks! 谢谢!

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. 我已经为delete事件实现了onKeyListener() ,为keys事件实现了onKeyListener()

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. TextWatcher方法有一个很大的缺点 - 您无法更改EditText链接的可编辑 - 它将导致循环。 Be careful! 小心!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM