97

你好我有一个搜索EditText和搜索Button。当我键入搜索的文本时,我想使用软键盘上的ENTER键而不是搜索Button来激活搜索功能。

提前感谢您的帮助。

4

8 回答 8

155

你可以通过OnKeyListener在你的EditText.

这是我自己的代码中的一个示例。我有一个EditTextnamed ,它会在单击 enter 键或 d-pad 时addCourseText调用该函数。addCourseFromTextBox

addCourseText = (EditText) findViewById(R.id.clEtAddCourse);
addCourseText.setOnKeyListener(new OnKeyListener()
{
    public boolean onKey(View v, int keyCode, KeyEvent event)
    {
        if (event.getAction() == KeyEvent.ACTION_DOWN)
        {
            switch (keyCode)
            {
                case KeyEvent.KEYCODE_DPAD_CENTER:
                case KeyEvent.KEYCODE_ENTER:
                    addCourseFromTextBox();
                    return true;
                default:
                    break;
            }
        }
        return false;
    }
});
于 2010-12-15T15:43:54.290 回答
46
<EditText
    android:id="@+id/search"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="@string/search_hint"
    android:inputType="text"
    android:imeOptions="actionSend" />

然后,您可以通过为 EditText 元素定义 TextView.OnEditorActionListener 来监听操作按钮上的按下。在您的侦听器中,响应 EditorInfo 类中定义的相应 IME 操作 ID,例如 IME_ACTION_SEND。例如:

EditText editText = (EditText) findViewById(R.id.search);
editText.setOnEditorActionListener(new OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        boolean handled = false;
        if (actionId == EditorInfo.IME_ACTION_SEND) {
            sendMessage();
            handled = true;
        }
        return handled;
    }
});

来源:https ://developer.android.com/training/keyboard-input/style.html

于 2014-03-19T09:58:11.193 回答
28

可能你可以像这样向你的 EditText 添加一个属性:

android:imeOptions="actionSearch"
于 2010-12-15T16:07:24.843 回答
6

向 EditText 添加一个属性,例如 android:imeOptions="actionSearch"

这是执行该功能的最佳方法

并且 imeOptions 也有一些其他的值,如 "go" 、"next"、"done" 等。

于 2011-01-24T12:20:24.333 回答
2

我们也可以使用 Kotlin lambda

editText.setOnKeyListener { _, keyCode, keyEvent ->
        if (keyEvent.action == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_ENTER) {
            Log.d("Android view component", "Enter button was pressed")
            return@setOnKeyListener true
        }
        return@setOnKeyListener false
    }
于 2018-12-19T15:22:13.187 回答
2

实现这一目标的最新方法是:

将此添加到 XML 中的 EditText:

android:imeOptions="actionSearch"

然后在您的活动/片段中:

EditText.setOnEditorActionListener { _, actionId, _ ->
    if (actionId == EditorInfo.IME_ACTION_SEARCH) {
        // Do what you want here
        return@setOnEditorActionListener true
    }
    return@setOnEditorActionListener false
}
于 2020-02-26T08:41:54.683 回答
1

这是我的一个应用程序的示例我如何处理

 //searching for the Edit Text in the view    
    final EditText myEditText =(EditText)view.findViewById(R.id.myEditText);
        myEditText.setOnKeyListener(new View.OnKeyListener() {
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                 if (event.getAction() == KeyEvent.ACTION_DOWN)
                      if ((keyCode == KeyEvent.KEYCODE_DPAD_CENTER) ||
                             (keyCode == KeyEvent.KEYCODE_ENTER)) {
                                //do something
                                //true because you handle the event
                                return true;
                               }
                       return false;
                       }
        });
于 2015-10-12T14:43:41.667 回答
0

为了避免焦点前进到下一个可编辑字段(如果有的话),您可能希望忽略按键事件,但处理按键事件。我也更喜欢先过滤 keyCode,假设它会稍微更有效率。顺便说一句,请记住,返回 true 意味着您已经处理了事件,因此其他侦听器不会。无论如何,这是我的版本。

ETFind.setOnKeyListener(new OnKeyListener()
{
    public boolean onKey(View v, int keyCode, KeyEvent event)
    {
        if (keyCode ==  KeyEvent.KEYCODE_DPAD_CENTER
        || keyCode ==  KeyEvent.KEYCODE_ENTER) {

            if (event.getAction() == KeyEvent.ACTION_DOWN) {
                // do nothing yet
            } else if (event.getAction() == KeyEvent.ACTION_UP) {
                        findForward();      
            } // is there any other option here?...

            // Regardless of what we did above,
            // we do not want to propagate the Enter key up
            // since it was our task to handle it.
            return true;

        } else {
            // it is not an Enter key - let others handle the event
            return false;
        }
    }

});
于 2013-04-26T08:24:27.273 回答