EditText需要设置两个属性:imeOptions = actionSearch和singleLine = true
//搜索监听
etSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
if (actionId == EditorInfo.IME_ACTION_SEARCH ||
(keyEvent != null && keyEvent.getKeyCode() == KeyEvent.KEYCODE_ENTER) ) {
//隐藏键盘
((InputMethodManager) etSearch.getContext()
.getSystemService(Context.INPUT_METHOD_SERVICE))
.hideSoftInputFromWindow(MainActivity.this
.getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
//这里进行搜索...
return true;
}
return false;
}
});