如何在android自定义InputMethodService上使用enter键进行搜索?

如何在android自定义InputMethodService上使用enter键进行搜索?,android,keyboard,android-softkeyboard,keyevent,android-input-method,Android,Keyboard,Android Softkeyboard,Keyevent,Android Input Method,我想用我的android自定义键盘按enter键进行搜索,但它不起作用。 我已经映射了这些键,我只需要在搜索文本字段上触发“搜索操作”,就像在谷歌上搜索一样 我尝试使用此代码触发搜索操作,但不起作用: ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER)); 以下是我覆盖enter key事件的方法: public class Keyboard extends InputMethodService

我想用我的android自定义键盘按enter键进行搜索,但它不起作用。
我已经映射了这些键,我只需要在搜索文本字段上触发“搜索操作”,就像在谷歌上搜索一样

我尝试使用此代码触发搜索操作,但不起作用:

ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER));
以下是我覆盖enter key事件的方法:

public class Keyboard extends InputMethodService
    implements KeyboardView.OnKeyboardActionListener {

@Override
public void onStartInputView(EditorInfo info, boolean restarting) {
    super.onStartInputView(info, restarting);

    setInputView(onCreateInputView());

    switch (EditorInfo.IME_MASK_ACTION | EditorInfo.IME_FLAG_NO_ENTER_ACTION) {

        case EditorInfo.IME_ACTION_SEARCH:
            Toast.makeText(this, "test", Toast.LENGTH_SHORT).show();
            //Action Search
            break;
    }
}
我的xml布局是:

<?xml version="1.0" encoding="UTF-8"?>
<android.inputmethodservice.KeyboardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/keyboard"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:keyPreviewHeight="60dp"
    android:keyPreviewOffset="12dp"
    android:keyPreviewLayout="@layout/preview"
    android:visibility="visible"/>


我的版面中没有任何EditText来设置android:imeOptions=“actionSearch”

您必须映射您的按键并覆盖enter键,然后将搜索代码放入其中

@Override
public void onKey(int primaryCode, int[] keyCodes) {     
    switch(primaryCode){

        case android.inputmethodservice.Keyboard.KEYCODE_DONE:
            ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER));
            break;
    break;
    }
}
如果您没有自定义密钥代码,您可以查看一些,然后尝试他的代码。

 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)) {
                // do ur stuff
                return true;
            }
            return false;
        }
    });

代码起作用了。但我没有任何编辑文本。在xml中,我有