【问题标题】:onEditorAction() is not called after Enter key has been pressed on Jelly Bean emulator在果冻豆模拟器上按下 Enter 键后不调用 onEditorAction()
【发布时间】:2012-07-03 22:28:57
【问题描述】:

我对最新的 Jelly Bean 模拟器的行为有疑问。我的应用中有几个EditTexts。当用户按下键盘上的 ENTER 键时,OnEditorActionListener 提供特殊处理。这在 ICS 之前一直有效,但现在在 Jelly Bean 上不再调用侦听器回调方法 onEditorAction()EditText 中只插入一个新行。

可以这样复制:

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);

这是果冻豆中的错误吗?还是在模拟器中?还是有意改变了行为?

奇怪的是,其他人在此处运行 Jelly Bean 的 Nexus 7 上调用了该方法,但带有意外参数:null keyevent and actionid = 0 in onEditorAction() (Jelly Bean / Nexus 7)

【问题讨论】:

  • 此示例项目的行为符合预期:github.com/commonsguy/cw-omnibus/tree/master/ActionBar/…
  • @CommonsWare:谢谢,我在您的代码中找到了解决问题的方法。如果我执行以下操作,则 ENTER 键与虚拟键盘上的 GO 键交换,这会触发 onEditorAction():editText.setRawInputType(InputType.TYPE_CLASS_TEXT); editText.setImeOptions(EditorInfo.IME_ACTION_GO);
  • 或者在 XML 中:android:imeOptions="actionGo" android:inputType="text"

标签: android android-edittext avd android-4.2-jelly-bean


【解决方案1】:

如果其他人发现这个问题:

我已经对此进行了多次测试,在 Jelly Bean 模拟器上,当在虚拟键盘上按下 Enter 键时,监听器回调方法 onEditorAction() 确实不再被调用。

如上所述,一种可能的解决方案或解决方法是将 Enter 键替换为可用的操作键之一。那些仍然触发 onEditorAction()。我还必须指定输入类型。

editText.setRawInputType(InputType.TYPE_CLASS_TEXT);
editText.setImeOptions(EditorInfo.IME_ACTION_GO);
<EditText
...
android:imeOptions="actionGo"
android:inputType="text" />

【讨论】:

  • 如果手机根本不支持imeActions怎么办? (有很多设备没有。)
【解决方案2】:

这是我所做的,它应该涵盖所有类型的 Enter 被按下:

override fun onEditorAction(v: TextView?, actionId: Int, event: KeyEvent?): Boolean {
    if (actionId == EditorInfo.IME_ACTION_DONE || actionId == EditorInfo.IME_NULL)
        ... // Enter pressed

在 XML 中我只添加了android:imeOptions="actionGo"

原因,根据文档:

https://developer.android.com/reference/android/widget/TextView.OnEditorActionListener.html#onEditorAction(android.widget.TextView,%20int,%20android.view.KeyEvent)

actionId int:动作的标识符。这将是 您提供的标识符,或 EditorInfo#IME_NULL 如果由于以下原因而被调用 按下回车键。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-29
    • 2016-05-17
    • 1970-01-01
    • 2012-07-05
    • 2012-08-09
    • 1970-01-01
    相关资源
    最近更新 更多