如何设置编辑文本以在键盘上显示搜索按钮或回车按钮



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

在布局中将输入法设置为"搜索"选项

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

使用代码编辑 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;
    }
});
  android:singleLine="true"
  android:imeOptions="actionSearch"

以下过程介绍如何使用 ArrayAdapter 设置从数组提供建议的自动完成文本视图:

1 - 将自动完成文本视图添加到布局中。下面是仅包含文本字段的布局:

<?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 - 在活动或片段中,使用以下代码指定提供建议的适配器:

// 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布局中存在的文本视图(这是 Android 提供的布局,为列表中的文本提供标准外观(。然后通过调用 setAdapter(( 将适配器分配给自动完成文本视图。

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

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

KOTLIN 开发人员:

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的内容。否则,您将无法在键盘打开时看到搜索图标。

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

<EditText android:inputType="number"/>

XML 文档

。或。。。

editText.setInputType(int);

代码文档

让你的编辑文本像这样。

    <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" />

使用这个

<EditText
                android:id="@+id/editSearch"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:imeOptions="actionSearch"
                android:singleLine="true"

必须在 xml 中添加 singleLine=true希望我的回答对您有所帮助

EditText 视图中设置此属性 "imeOptions":

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

在 Kotlin 文件中

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

在 XML 文件中

android:imeOptions="actionSearch"
           

最新更新