简体   繁体   English

用于软键盘的View.OnKeyListener

[英]View.OnKeyListener for soft keyboard

I am using the View.OnKeyListener for EditText to do something when some special keys are pressed/touched. 我正在使用View.OnKeyListener for EditText在按下/触摸某些特殊键时执行某些操作。 Here is a shorthened version of the code: 这是代码的缩写版本:

public class MyKeyListener implements View.OnKeyListener{
    // ...

    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {

        if (event.getAction() == KeyEvent.ACTION_UP) {
            if (keyCode == KeyEvent.KEYCODE_DEL) {
                // do some processing
                return true;
            } else if (keyCode == KeyEvent.KEYCODE_0) {
                // do another processing
                return true;
            }
        }
    }
}

The problem is that the OnKeyListener, according to the documentation, is only guaranteed to be called when hard keyboards are used. 问题是根据文档,OnKeyListener只保证在使用硬键盘时被调用。 And, indeed, I tested it on some phones where it doesn't work for soft keyboards. 事实上,我在一些不适用于软键盘的手机上进行了测试。 So I was wondering if there is something similar to this listener that would work for soft keyboards. 所以我想知道是否有类似于这个听众的东西适用于软键盘。

It's not guaranteed, you can't. 它不能保证,你不能。

You can use a TextWatcher and attach that to any and all EditTexts. 您可以使用TextWatcher并将其附加到任何和所有EditTexts。

Or, you create your own KeyboardView and force the user to use that one, then you can attach the expected listeners to it. 或者,您创建自己的KeyboardView并强制用户使用它,然后您可以将预期的侦听器附加到它。 https://developer.android.com/guide/topics/text/creating-input-method.html?hl=ja#IMEUI https://developer.android.com/guide/topics/text/creating-input-method.html?hl=ja#IMEUI

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

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