Android 在 Jelly Bean 模拟器上按下 Enter 键后不会调用 onEditorAction()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11311790/
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
onEditorAction() is not called after Enter key has been pressed on Jelly Bean emulator
提问by Hendrik
I'm having a problem with the behavior of the latest Jelly Bean emulator. I have several EditTexts
in my app. An OnEditorActionListener
provides special handling when a user presses the ENTER key on the keyboard. This worked up until ICS, but now on Jelly Bean the listener callback method onEditorAction()
no longer gets called. Only a new line is inserted into the EditText
.
我对最新的 Jelly Bean 模拟器的行为有疑问。EditTexts
我的应用程序中有几个。OnEditorActionListener
当用户按下键盘上的 ENTER 键时,An提供特殊处理。这在 ICS 之前一直有效,但现在在 Jelly Bean 上onEditorAction()
不再调用侦听器回调方法。只插入一个新行到EditText
.
This can be reproduced this way:
这可以通过这种方式重现:
EditText testEditText = new EditText(context);
testEditText.setOnEditorActionListener(new OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
Log.d(TAG, "onEditorAction() called");
return false;
}
});
addView(testEditText);
Is this a bug in Jelly Bean? Or in the emulator? Or has the behavior been changed intentionally?
这是果冻豆中的错误吗?还是在模拟器里?还是故意改变了行为?
Curiously someone else writes that the method gets called, but with unexpected parameters, on a Nexus 7 running Jelly Bean here: null keyevent and actionid = 0 in onEditorAction() (Jelly Bean / Nexus 7)
奇怪的是,其他人写道,在运行 Jelly Bean 的 Nexus 7 上,该方法被调用,但参数意外:null keyevent 和 actionid = 0 in onEditorAction() (Jelly Bean / Nexus 7)
回答by Hendrik
If someone else finds this question:
如果其他人发现这个问题:
I've tested this several times and on the Jelly Bean emulator the listener callback method onEditorAction() indeed no longer gets called when the Enter key is pressed on the virtual keyboard.
我已经对此进行了多次测试,并且在 Jelly Bean 模拟器上,当在虚拟键盘上按下 Enter 键时,监听器回调方法 onEditorAction() 确实不再被调用。
As I mentioned above a possible solution or workaround is to replace the Enter key with one of the available action keys. Those still trigger onEditorAction(). I also had to specify the input type.
正如我上面提到的,一个可能的解决方案或变通方法是用可用的操作键之一替换 Enter 键。那些仍然会触发 onEditorAction()。我还必须指定输入类型。
editText.setRawInputType(InputType.TYPE_CLASS_TEXT);
editText.setImeOptions(EditorInfo.IME_ACTION_GO);
<EditText
...
android:imeOptions="actionGo"
android:inputType="text" />
回答by android developer
Here's what I did, which should cover all types of the Enter being pressed:
这是我所做的,它应该涵盖所有类型的 Enter 被按下:
override fun onEditorAction(v: TextView?, actionId: Int, event: KeyEvent?): Boolean {
if (actionId == EditorInfo.IME_ACTION_DONE || actionId == EditorInfo.IME_NULL)
... // Enter pressed
In XML I added only android:imeOptions="actionGo"
在 XML 中我只添加了 android:imeOptions="actionGo"
The reason, according to the docs:
原因,根据文档:
actionId int: Identifier of the action. This will be either the identifier you supplied, or EditorInfo#IME_NULL if being called due to the enter key being pressed.
actionId int:动作的标识符。这将是您提供的标识符,或者是 EditorInfo#IME_NULL(如果因按下 Enter 键而被调用)。