onKeyListener 不适用于虚拟键盘

发布于 2024-10-04 03:00:17 字数 587 浏览 8 评论 0原文

我不明白为什么这段代码不起作用。仅检测到退格键和返回键。 Listener 不会因任何其他键而触发。我的设备是 Nexus One。

我尝试重写 Activity 的 OnKeyDown 方法,结果更糟。唯一检测到的按钮是硬件后退按钮。

我看到有人建议使用 TextWatcher 和 onTextChanged,虽然这在某些情况下可能有效,但这并不是一个真正的解决方法。例如,如果文本框为空,您将不会检测用户是否按下 BackSpace(删除)按钮。 那么有什么想法吗?

        TextView txtInput = (TextView)findViewById(R.id.txtInput);
    txtInput.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            makeToast(keyCode + " key pressed");
            return true;
        }
    });

I don't understand why this piece of code is not working. Only backspace and return key are detected. Listener doesn't fire for any other key. My device is Nexus One.

I tried to override activity's OnKeyDown method and that's even worse. The only detected button was hardware back button.

I am seeing around a suggestion to use TextWatcher and onTextChanged, while that might work in some cases, it's not a real work around. For example, if textbox is empty, you won't detect if user press BackSpace(Delete) button.
So any ideas?

        TextView txtInput = (TextView)findViewById(R.id.txtInput);
    txtInput.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            makeToast(keyCode + " key pressed");
            return true;
        }
    });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

只是偏爱你 2024-10-11 03:00:17

好的。我终于想出了如何做我想做的事,我并不因此而对 Android 感到自豪。

我正在编写服务器/客户端应用程序,在客户端上我必须打开软键盘并发送按下的按键(字符和 DEL 键)...每次按下按键时,该字符都会发送到服务器。如果按下 DEL,我将向服务器发送序列 {BS}。

为了做到这一点,我必须实现 TextWatcher 和 onTextChange ,除了 EditText 为空且用户按 DEL 键的情况外,它们运行良好。由于 EditText 没有变化,因此无法检测到 DEL 键被按下。

除了 TextWatcher 之外,我还必须实现附加到 EditText 控件的 onKeyListener。此 onKeyListener 忽略软键盘上除 DEL 和 RETURN 之外的所有键。不知道为什么?也许是一个错误?

这是我的代码:

    TextView txtInput = (TextView)findViewById(R.id.txtInput);
    txtInput.addTextChangedListener(inputTextWatcher);

    txtInput.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            Log.d(TAG, keyCode + " character(code) to send");
            return false;
        }
    });

和 TextWatcher....

private TextWatcher inputTextWatcher = new TextWatcher() {
    public void afterTextChanged(Editable s) { }
    public void beforeTextChanged(CharSequence s, int start, int count, int after)
        { }
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        Log.d(TAG, s.charAt(count-1) + " character to send");;          
    }
};

Ok. I finally figured how to do what I want, and I am not proud on Android for this.

I am writing server/client application where on client I have to open SoftKeyboard and send pressed keys (characters and DEL key)... Each time key is pressed, that character is sent to server. If DEL is pressed I am sending sequence {BS} to server.

In order to do that I had to implement TextWatcher and onTextChange which works well except for situation when EditText is empty and user press DEL key. Since there is no change in EditText there is no way to detect that DEL key is pressed.

In addition to TextWatcher, I had to implement onKeyListener which I attached to my EditText control. This onKeyListener ignores all keys on SoftKeyboard except DEL and RETURN. Not sure why? A bug maybe?

Here is my code:

    TextView txtInput = (TextView)findViewById(R.id.txtInput);
    txtInput.addTextChangedListener(inputTextWatcher);

    txtInput.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            Log.d(TAG, keyCode + " character(code) to send");
            return false;
        }
    });

and TextWatcher....

private TextWatcher inputTextWatcher = new TextWatcher() {
    public void afterTextChanged(Editable s) { }
    public void beforeTextChanged(CharSequence s, int start, int count, int after)
        { }
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        Log.d(TAG, s.charAt(count-1) + " character to send");;          
    }
};
人间☆小暴躁 2024-10-11 03:00:17

你在这里犯了一个错误。
如果您处理了该事件,它应该返回 true。如果您想允许下一个接收者处理该事件返回 false
你总是返回 true

You have done one mistake here.
it should return true,If you handled the event. If you want to allow the event to be handled by the next receiver, return false
You are always returning true

只有一腔孤勇 2024-10-11 03:00:17

注意:编辑文本中提到的输入类型。

<EditText android:id="@+id/select_category" 
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textCapSentences|textAutoCorrect" >



edittext.setOnEditorActionListener(new OnEditorActionListener() {

            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {

                if ((actionId & EditorInfo.IME_MASK_ACTION) == EditorInfo.IME_ACTION_DONE) {
                    //do something here.
                    return true;
                }
                return false;
            }
        });

Note : inputtype mention in your edittext.

<EditText android:id="@+id/select_category" 
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textCapSentences|textAutoCorrect" >



edittext.setOnEditorActionListener(new OnEditorActionListener() {

            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {

                if ((actionId & EditorInfo.IME_MASK_ACTION) == EditorInfo.IME_ACTION_DONE) {
                    //do something here.
                    return true;
                }
                return false;
            }
        });
黑色毁心梦 2024-10-11 03:00:17

您是否尝试过使用该视图的特定侦听器子类?

我对 EditText 也有类似的问题。下面的代码有效:

private OnKeyListener keyListener = new EditText.OnKeyListener(){

    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        EditText et = (EditText) v;
        Log.i("APPEVENT", "Key hit: "+ event);
        //...
        return false;
    }

};

但是如果我使用View.OnKeyListener,我只记录退格键并输入。 TextView 可能存在类似的问题。也许子类键侦听器对给定 View 子类的更多事件敏感。

Have you tried using the specific listener sub-class for that view?

I had a similar problem with an EditText. The code below worked:

private OnKeyListener keyListener = new EditText.OnKeyListener(){

    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        EditText et = (EditText) v;
        Log.i("APPEVENT", "Key hit: "+ event);
        //...
        return false;
    }

};

But if I used the View.OnKeyListener, I only record backspace and enter. It could be a similar problem with TextView. Perhaps the subclass key listeners are sensitive to more events for the given View subclass.

若无相欠,怎会相见 2024-10-11 03:00:17

引用自:

http://developer.android.com/reference/android /view/View.OnKeyListener.html

View.OnKeyListener

类概述 当硬件按键事件分派到此视图时要调用的回调的接口定义。回调将在按键事件传递给视图之前调用。这仅对硬件键盘有用;软件输入法没有义务触发此侦听器。

看来 OnKeyListener 是专为硬件按键设计的!

Referencing from:

http://developer.android.com/reference/android/view/View.OnKeyListener.html

View.OnKeyListener

Class Overview Interface definition for a callback to be invoked when a hardware key event is dispatched to this view. The callback will be invoked before the key event is given to the view. This is only useful for hardware keyboards; a software input method has no obligation to trigger this listener.

It seems OnKeyListener was designed for HARDWARE keys only!

怀里藏娇 2024-10-11 03:00:17

我们在 iPhone 上也遇到了 TextWatcher 问题——如果缓冲区为空,键盘不会发送 del 事件。我们通过在键盘缓冲区中预加载 1000 个字符来解决这个问题。幸运的是,我们的应用程序将编辑字段隐藏在键盘后面,因此看不到 1000 个字符。它很丑陋,但它有效(除非用户在输入任何数据之前连续删除 1000 次!)

We had the TextWatcher problem on iPhone as well -- if buffer is empty, keyboard does not send del event. We worked around it by preloading the keyboard buffer with 1000 characters. Luckily, our app hid the edit field behind the keyboard so the 1000 characters are not seen. It's ugly, but it works (unless the user hits 1000 deletes in a a row prior to entering any data!)

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