Android:如何让键盘输入按钮说"搜索"并处理其点击?

Ric*_*ket 359 android android-softkeyboard

我无法弄清楚这一点.有些应用程序有一个EditText(文本框),当你触摸它并调出屏幕键盘时,键盘上有一个"搜索"按钮而不是一个回车键.

我想实现这个.如何实现"搜索"按钮并检测按"搜索"按钮?

编辑:找到如何实现搜索按钮; 用XML android:imeOptions="actionSearch"或Java EditTextSample.setImeOptions(EditorInfo.IME_ACTION_SEARCH);.但是如何处理用户按下"搜索"按钮?它与之有关android:imeActionId吗?

Rob*_*ond 869

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

<EditText
    android:imeOptions="actionSearch" 
    android:inputType="text" />
Run Code Online (Sandbox Code Playgroud)

在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;
    }
});
Run Code Online (Sandbox Code Playgroud)

  • 在os 2.3.6上,直到我输入android:inputType ="text"属性才行. (78认同)
  • 在Android 2.3.5和4.0.4上我也需要android:inputType ="text" (41认同)
  • android:inputType ="text"也是4.4.0 - 4.4.2(Android Kitkat)所必需的. (13认同)
  • 是的,android:inputType ="text"仍然需要5.0 :) (12认同)
  • @Carol`EditText`是`TextView`的子类. (6认同)
  • android:inputType="text" 在 7.0 (Android Nougat) 上也是必需的 (2认同)

小智 15

用户单击搜索时隐藏键盘.除了Robby Pond回答

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


Sha*_*aon 10

在科特林

evLoginPassword.setOnEditorActionListener { _, actionId, _ ->
    if (actionId == EditorInfo.IME_ACTION_DONE) {
        doTheLoginWork()
    }
    true
}
Run Code Online (Sandbox Code Playgroud)

部分 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>
Run Code Online (Sandbox Code Playgroud)


Bra*_*ngh 6

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" />
Run Code Online (Sandbox Code Playgroud)