如何设置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 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"
<?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。
设置这两个字段以在键盘上显示搜索图标。
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 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
的任何内容。否则,您将无法在打开键盘时看到搜索图标。
Kotlin 文件中
ed_search.setOnEditorActionListener(TextView.OnEditorActionListener { v, actionId, event ->
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
}
false
})
在 XML 文件中
android:imeOptions="actionSearch"
<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视图中设置这个属性"imeOptions":
<EditText
android:imeOptions="actionSearch"
android:layout_width="match_parent"
android:inputType="text"
android:hint="Enter search"
android:layout_height="wrap_content"
/>
android:inputType="text"
才能使其正常工作。 - Bojan Radivojevicandroid:inputType="text"
。(我认为是针对ICS及以上版本的) - Ajmal SalimKeyEvent.KEYCODE_ENTER
。 - JStephen