简体   繁体   English

onKeyListener无法使用软键盘(Android)

[英]onKeyListener not working with soft keyboard (Android)

I am using onKeyListener to get the onKey events. 我正在使用onKeyListener来获取onKey事件。 It works fine with the normal keyboard. 它可以正常使用普通键盘。 But it does not work with soft keyboard. 但它不适用于软键盘。 I am only able to get onKey events for numerics and not alphabets. 我只能获得数字而非字母的关键事件。 Is there any workaround to solve this? 有没有解决方法可以解决这个问题? Any kind of help will be greatly appreciated. 任何形式的帮助将不胜感激。

I don't believe an OnKeyListener gets called at all with the software keyboard. 我完全不相信使用软件键盘调用OnKeyListener。 It has something to do with the software keyboard being an IME device and IME devices possibly being things other than keyboards. 它与软件键盘是IME设备有关,IME设备可能是键盘以外的东西。 It seems to make onKeyListener pretty much useless though, since it only works on phones with hardware keyboards. 它似乎使onKeyListener几乎无用,因为它只适用于带有硬件键盘的手机。 I worked around this issue recently by using TextWatcher on the EditText field in my Activity instead of using OnKeyListener. 我最近工作解决此问题,通过使用TextWatcher在我的活动上的EditText场,而不是使用OnKeyListener。

onKeyListener worked perfectly on Android 1.5 via the soft keyboard onKeyListener通过软键盘在Android 1.5上完美运行

From Android 1.6 onwards the character and number keys are not going via the onKey event, yet the DEL key does 从Android 1.6开始,字符和数字键不会通过onKey事件,但DEL键会发生

Frustrating 泄气

This is probably stupid, but that's how Android works at the moment. 这可能是愚蠢的,但这就是Android目前的工作方式。

The documentation states that the key events will only be propagated for the hardware key strokes, not software. 该文档指出,关键事件只会传播硬件键击,而不是软件。

The device manufacturers are actually being discouraged to propagate soft keyboard events through key listeners, although it is completely up to the manufacturer to honour that or to actually treat the soft and hard keyboards with equal terms. 实际上,设备制造商不鼓励通过关键听众传播软键盘事件,尽管完全由制造商来尊重或者用相同的术语实际处理软键盘和硬键盘。

Starting from Android 4.2.2, Android system itself will not support key stoke events for the soft keyboards at all, so even the manufacturers will not be able to choose their way. 从Android 4.2.2开始,Android系统本身根本不支持软键盘的关键加速事件,因此即使是制造商也无法选择自己的方式。

So the only foolproof option here is to implement your own IME (soft keyboard), and handle the keystrokes yourself. 因此,这里唯一的万无一失的选择是实现自己的IME(软键盘),并自己处理击键。

TextWatcher can be used mostly to replace the key listeners, however editText.setText(...); TextWatcher主要用于替换键侦听器,但是editText.setText(...); will also trigger the TextWatcher events, so if one is interested in typed keys only then probably TextWatcher is not a solution either. 也会触发TextWatcher事件,所以如果一个人对键入的键感兴趣,那么TextWatcher可能也不是解决方案。

Please be cautious when using TextWatcher with AutocomleteTextView or EditText. 将TextWatcher与AutocomleteTextView或EditText一起使用时请小心谨慎。 Do not modify text in the AutocompleteTextView / EditText's content from within TextWatcher events, cause otherwise you'll most probably end up in an infinite event/listening loop. 不要在TextWatcher事件中修改AutocompleteTextView / EditText内容中的文本,否则您最有可能最终进入无限事件/侦听循环。

Hope this helps to clarify the available options, but sadly it does not provide a working solution. 希望这有助于澄清可用的选项,但遗憾的是它没有提供可行的解决方案。

Disappointing that Google has missed on this important aspect of their UI. 令人失望的是谷歌错过了他们UI的这个重要方面。

This seems to be device specific. 这似乎是特定于设备的。 I can confirm that this works on the Xoom and the Acer A100. 我可以确认这适用于Xoom和Acer A100。 However, the Samsung Galaxy Tab Plus only fires the event for the non-character buttons. 但是,三星Galaxy Tab Plus仅针对非字符按钮触发事件。 (All devices running Honeycomb) (所有运行Honeycomb的设备)

I got around this by putting the listener into it's own method and calling it again after the first time. 我通过将监听器放入其自己的方法并在第一次之后再次调用它来解决这个问题。 In the onCreate I call setKeyListenerForEnter(); 在onCreate中我调用setKeyListenerForEnter();

Then, here's the method: 然后,这是方法:

public void setKeyListenerForEnter(){ public void setKeyListenerForEnter(){

    final EditText search_entry = (EditText) findViewById(R.id.search_entry);
    search_entry.setOnKeyListener(new OnKeyListener() {
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            // If the event is a key-down event on the "enter" button
            if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
                (keyCode == KeyEvent.KEYCODE_ENTER)) {
                getSearchResults(v);
                setKeyListenerForEnter();
              return true;
            }
            return false;
        }
    });
}

I'm not sure if this is a better solution than handling the IME keyboard itself, but it is a solution. 我不确定这是否比处理IME键盘本身更好,但它是一个解决方案。

setFocusableInTouchMode(true); //Enable soft keyboard on touch for target view

setFocusable(true); //Enable hard keyboard to target view

example: 例:

public class CanvasView extends View{
    public CanvasView(Context c){
        super(c);

        //enable keyboard
        setOnKeyListener(new KeyBoard());
        setFocusable(true);
        setFocusableInTouchMode(true);
    }
} 

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

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