1、xml配置
android:imeOptions="actionSearch"
android:singleLine="true"
<EditText
android:id="@+id/edt_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@color/white"
android:hint="搜索姓名/工号"
android:imeOptions="actionSearch"
android:singleLine="true"
android:textColor="@color/color_333"
android:textSize="14sp" />
2、使用
private void initSearch() {
//搜索
mEdtSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
if (i == EditorInfo.IME_ACTION_SEARCH) {
// 当按了搜索之后关闭软键盘
((InputMethodManager) mEdtSearch.getContext().getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(
AddTaskActivity.this.getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
mSearchKey = mEdtSearch.getText().toString().trim();
getPersonListData();
}
return false;
}
});
}
或者
//搜索
editText.setOnEditorActionListener((textView, i, keyEvent) -> {
if (i == EditorInfo.IME_ACTION_SEARCH) {
mIvSearch.performClick(); //某个按钮点击事件
}
return false;
});
隐藏软键盘
/**
* 隐藏键盘
*/
protected void hideInput(Activity activity) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
}