关键在于:,解决方法是调用setOnEditorActionListener而不是用setOnKeyListener来监听点击搜索按钮。
第一种写法:
circleEtSousuo.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View view, int i, KeyEvent keyEvent) {
if (i == KeyEvent.KEYCODE_ENTER) {
//先隐藏键盘
((InputMethodManager) SearchActivity.this.getSystemService(INPUT_METHOD_SERVICE))
.hideSoftInputFromWindow(SearchActivity.this.getCurrentFocus()
.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
//进行搜索操作的方法,在该方法中可以加入mEditSearchUser的非空判断
intent.setClass(SearchActivity.this, MyCollectActivity.class);
startActivity(intent);
}
return false;
}
});*/
修正以后解决问题:
circleEtSousuo.setOnEditorActionListener(newTextView.OnEditorActionListener() {
@Override
public booleanonEditorAction(TextView textView,inti, KeyEvent keyEvent) {
if(i == EditorInfo.IME_ACTION_SEARCH) {
//先隐藏键盘
((InputMethodManager) SearchActivity.this.getSystemService(Context.INPUT_METHOD_SERVICE))
.hideSoftInputFromWindow(
SearchActivity.this
.getCurrentFocus()
.getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
search();
return true;
}
return false;
}
});