Android:如何让键盘输入按钮说“搜索”并处理它的点击?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3205339/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:08:55  来源:igfitidea点击:

Android: how to make keyboard enter button say "Search" and handle its click?

androidandroid-softkeyboard

提问by Ricket

I can't figure this out. Some apps have a EditText (textbox) which, when you touch it and it brings up the on-screen keyboard, the keyboard has a "Search" button instead of an enter key.

我想不通。一些应用程序有一个 EditText(文本框),当你触摸它并打开屏幕键盘时,键盘有一个“搜索”按钮而不是输入键。

I want to implement this. How can I implement that Search button and detect press of the Search button?

我想实现这一点。如何实现该“搜索”按钮并检测“搜索”按钮的按下情况?

Edit: found how to implement the Search button; in XML, android:imeOptions="actionSearch"or in Java, EditTextSample.setImeOptions(EditorInfo.IME_ACTION_SEARCH);. But how do I handle the user pressing that Search button? Does it have something to do with android:imeActionId?

编辑:找到了如何实现搜索按钮;在 XMLandroid:imeOptions="actionSearch"或 Java 中,EditTextSample.setImeOptions(EditorInfo.IME_ACTION_SEARCH);. 但是我如何处理用户按下该“搜索”按钮?和它有关系android:imeActionId吗?

回答by Robby Pond

In the layout set your input method options to search.

在布局中设置要搜索的输入法选项。

<EditText
    android:imeOptions="actionSearch" 
    android:inputType="text" />

In the java add the editor action listener.

在java中添加编辑器动作监听器。

editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_SEARCH) {
            performSearch();
            return true;
        }
        return false;
    }
});

回答by kaMChy

Hide keyboard when user clicks search. Addition to Robby Pond answer

当用户点击搜索时隐藏键盘。除了 Robby Pond 答案

private void performSearch() {
    editText.clearFocus();
    InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    in.hideSoftInputFromWindow(searchEditText.getWindowToken(), 0);
    //...perform search
}

回答by Braj Bhushan Singh

In xmlfile, put imeOptions="actionSearch"and inputType="text", maxLines="1":

xml文件中,放入imeOptions="actionSearch"inputType="text", maxLines="1"

<EditText
    android:id="@+id/search_box"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/search"
    android:imeOptions="actionSearch"
    android:inputType="text"
    android:maxLines="1" />

回答by Shaon

In Kotlin

在科特林

evLoginPassword.setOnEditorActionListener { _, actionId, _ ->
    if (actionId == EditorInfo.IME_ACTION_DONE) {
        doTheLoginWork()
    }
    true
}

Partial Xml Code

部分 XML 代码

 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
       <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            android:layout_marginBottom="8dp"
            android:layout_marginTop="8dp"
            android:paddingLeft="24dp"
            android:paddingRight="24dp">

            <EditText
                android:id="@+id/evLoginUserEmail"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/email"
                android:inputType="textEmailAddress"
                android:textColor="@color/black_54_percent" />
        </android.support.design.widget.TextInputLayout>

        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:layout_marginTop="8dp"
            android:paddingLeft="24dp"
            android:paddingRight="24dp">

            <EditText
                android:id="@+id/evLoginPassword"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/password"
                android:inputType="textPassword"
                android:imeOptions="actionDone"
                android:textColor="@color/black_54_percent" />
        </android.support.design.widget.TextInputLayout>
</LinearLayout>

回答by iAmSauravSharan

This answer is for TextInputEditText :

此答案适用于 TextInputEditText :

In the layout XML file set your input method options to your required type. for example done.

在布局 XML 文件中,将输入法选项设置为所需类型。例如完成

<com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:imeOptions="actionGo"/>

Similarly, you can also set imeOptions to actionSubmit, actionSearch, etc

同样,你也可以将imeOptions设置为actionSubmit、actionSearch等

In the java add the editor action listener.

在java中添加编辑器动作监听器。

textInputLayout.getEditText().setOnEditorActionListener(new 

    TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_GO) {
                performYourAction();
                return true;
            }
            return false;
        }
    });

If you're using kotlin :

如果您使用 kotlin :

textInputLayout.editText.setOnEditorActionListener { _, actionId, _ ->
    if (actionId == EditorInfo.IME_ACTION_GO) {
        performYourAction()
    }
    true
}

回答by Null Pointer Exception

by XML:

通过 XML:

 <EditText
        android:id="@+id/search_edit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/search"
        android:imeOptions="actionSearch"
        android:inputType="text" />

By Java:

通过 Java:

 editText.clearFocus();
    InputMethodManager in = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    in.hideSoftInputFromWindow(searchEditText.getWindowToken(), 0);