如何将EditText设置为在键盘上显示搜索按钮或回车按钮?

68

如何设置EditText以显示搜索按钮或回车键在键盘上?

11个回答

221

在您的布局中,将输入法选项设置为“搜索”。

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

在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;
    }
});

38
我必须为我的EditText设置android:inputType="text"才能使其正常工作。 - Bojan Radivojevic
3
是的,对于较高的API级别,你需要添加android:inputType="text"。(我认为是针对ICS及以上版本的) - Ajmal Salim
1
请注意,如果用户使用外部键盘,他们可能没有搜索按钮。如果您没有其他执行搜索的方法,您还可以检查 KeyEvent.KEYCODE_ENTER - JStephen
如果不将KeyEvent设置为可选项,这段代码会导致崩溃。参考:https://dev59.com/ZlYN5IYBdhLWcg3wLFiI - Shahen Kosyan

112
使用这段代码来编辑EditText属性。
<EditText android:imeOptions="actionSearch" />

然后在你的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;
    }
});

运作正常。 在“搜索”操作中调用了API。 - BK19

18
  android:singleLine="true"
  android:imeOptions="actionSearch"

3
以下步骤描述了如何设置AutoCompleteTextView,从一个数组中提供建议,并使用ArrayAdapter: 1 - 将AutoCompleteTextView添加到您的布局中。这是一个只有文本字段的布局:
<?xml version="1.0" encoding="utf-8"?>
<AutoCompleteTextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/autocomplete_country"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

2 - 定义包含所有文本建议的数组。例如,以下是在XML资源文件(res/values/strings.xml)中定义的国家名称数组:

    <?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="countries_array">
        <item>Afghanistan</item>
        <item>Albania</item>
        <item>Algeria</item>
        <item>American Samoa</item>
        <item>Andorra</item>
        <item>Angola</item>
        <item>Anguilla</item>
        <item>Antarctica</item>
        ...
    </string-array>
</resources>

3 - 在您的Activity或Fragment中,使用以下代码指定提供建议的适配器:

// Get a reference to the AutoCompleteTextView in the layout
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete_country);
// Get the string array
String[] countries = getResources().getStringArray(R.array.countries_array);
// Create the adapter and set it to the AutoCompleteTextView 
ArrayAdapter<String> adapter = 
        new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, countries);
textView.setAdapter(adapter);

这里初始化了一个新的ArrayAdapter,将COUNTRIES字符串数组中的每个项目绑定到simple_list_item_1布局中存在的TextView上(这是Android提供的为列表中的文本提供标准外观的布局)。 然后通过调用setAdapter()将适配器分配给AutoCompleteTextView。


3

设置这两个字段以在键盘上显示搜索图标。

            android:imeOptions="actionSearch"
            android:imeActionLabel="@string/search"

如果您需要在键盘搜索按钮单击时执行某些操作,则需要添加以下代码。

    etSearch.setOnEditorActionListener((v, actionId, event) -> {
        if (actionId == EditorInfo.IME_ACTION_SEARCH) {
              search();     // you can do anything
            return true;
        }
        return false;
    });

1

KOTLIN DEVELOPERS:

1. XML:

        <androidx.appcompat.widget.AppCompatEditText
            android:id="@+id/etSearch"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="@font/sans_medium"
            android:imeOptions="actionSearch"
            android:inputType="text" />

2. 类文件:

etSearch.setOnEditorActionListener(object : TextView.OnEditorActionListener {
        override fun onEditorAction(v: TextView?, actionId: Int, event: KeyEvent?): Boolean {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                //hide Keyboard
                //do search here
                return true
            }
            return false
        }
    })

注:当使用imeOptions="actionSearch"时,请不要忘记在您的XML中添加inputType="text"或您想设置为inputType的任何内容。否则,您将无法在打开键盘时看到搜索图标。


可以运行,但我不需要输入文本类型。 - Arjun Verma
@ArjunVerma 请尝试使用不同的设备! - Kishan Solanki

0

您可以通过使用EditText的inputType来更改可用的键盘。

<EditText android:inputType="number"/>

XML文档

...或者...

editText.setInputType(int);

代码文档


0

Kotlin 文件中

ed_search.setOnEditorActionListener(TextView.OnEditorActionListener { v, actionId, event ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
               
            }
            false
        })

在 XML 文件中

android:imeOptions="actionSearch"
           

0
将你的EditText设置成这样。
    <EditText
        android:id="@+id/s_search"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#000"
        android:textSize="15dp"
        android:imeOptions="actionSearch"
        android:inputType="text"
        android:hint="Enter search" />

0

在EditText视图中设置这个属性"imeOptions":

<EditText
     android:imeOptions="actionSearch"
     android:layout_width="match_parent"
     android:inputType="text"
     android:hint="Enter search"
     android:layout_height="wrap_content"
/>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接