如何设置edittext以显示搜索按钮或在键盘上输入按钮?

use*_*156 53 keyboard android

如何设置EditText显示搜索按钮或在键盘上输入按钮?

Rah*_*rma 202

在您的布局设置输入方法到搜索选项

 <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)

  • 我必须为我的edittext设置`android:inputType ="text"`以使其工作 (36认同)
  • 是的,您需要添加`android:inputType ="text"`以获得更高的API级别.(对于ICS +我认为) (3认同)

suc*_*nil 80

使用该代码编辑EditText属性

<EditText android:imeOptions="actionSearch" />
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)


Suj*_*ana 16

  android:singleLine="true"
  android:imeOptions="actionSearch"
Run Code Online (Sandbox Code Playgroud)