Android setOnEditorActionListener 不工作

Android setOnEditorActionListener not working

出于某种原因,setOnEditorActionListener 无法正常工作。

我在22的target sdk上

这是我的 onCreate 方法中的代码:

final EditText pageBox = (EditText) findViewById(R.id.pageBox);

    searchBox.setOnEditorActionListener(new OnEditorActionListener() {
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) || (actionId == EditorInfo.IME_ACTION_DONE)) {

        }
      }
    });

这里是 xml:

<EditText
   android:layout_width="0dp"
   android:layout_height="wrap_content"
   android:id="@+id/searchBox"
   android:singleLine="true"
   android:hint="@string/string_find_player"
   android:layout_gravity="left"
   android:layout_weight="1" />

非常感谢所有帮助!

您应该尝试将 android:imeOptions="actionDone" 添加到您的 xml 中,因为您正在寻找该代码 (EditorInfo.IME_ACTION_DONE):

<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/searchBox"
android:singleLine="true"
android:hint="@string/string_find_player"
android:layout_gravity="left"
android:layout_weight="1"
android:imeOptions="actionDone" />

searchBox是什么对象,可能你得给pageBox

添加监听器