onKeyListener 不适用于软键盘(Android)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1967740/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
onKeyListener not working with soft keyboard (Android)
提问by
I am using onKeyListener to get the onKey events. 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.
我正在使用 onKeyListener 来获取 onKey 事件。它在普通键盘上工作正常。但它不适用于软键盘。我只能获得数字而不是字母的 onKey 事件。有什么解决方法可以解决这个问题吗?任何形式的帮助将不胜感激。
回答by Mark B
I don't believe an OnKeyListener gets called at all with the software keyboard. It has something to do with the software keyboard being an IME device and IME devices possibly being things other than keyboards. It seems to make onKeyListener pretty much useless though, since it only works on phones with hardware keyboards. I worked around this issue recently by using TextWatcheron the EditText field in my Activity instead of using OnKeyListener.
我不相信 OnKeyListener 会被软件键盘调用。它与作为 IME 设备的软件键盘和可能是键盘以外的东西的 IME 设备有关。不过,它似乎使 onKeyListener 几乎毫无用处,因为它仅适用于带有硬件键盘的手机。我最近工作解决此问题,通过使用TextWatcher在我的活动上的EditText场,而不是使用OnKeyListener。
回答by Martin
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
令人沮丧
回答by Nar Gar
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(...); will also trigger the TextWatcher events, so if one is interested in typed keys only then probably TextWatcher is not a solution either.
TextWatcher 主要用于替换关键侦听器,但是 editText.setText(...); 也会触发 TextWatcher 事件,所以如果一个人只对键入的键感兴趣,那么 TextWatcher 也不是一个解决方案。
Please be cautious when using TextWatcher with AutocomleteTextView or 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 与 AutocomleteTextView 或 EditText 一起使用时请小心。不要在 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.
令人失望的是,谷歌错过了他们用户界面的这一重要方面。
回答by Mark D
This seems to be device specific. I can confirm that this works on the Xoom and the Acer A100. However, the Samsung Galaxy Tab Plus only fires the event for the non-character buttons. (All devices running Honeycomb)
这似乎是特定于设备的。我可以确认这适用于 Xoom 和 Acer A100。但是,三星 Galaxy Tab Plus 只为非字符按钮触发事件。(所有运行 Honeycomb 的设备)
回答by Funkavenger
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(){
公共无效 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 键盘本身更好的解决方案,但它是一个解决方案。
回答by user3309567
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);
}
}