效果:
一.设置显示搜索按钮
在xml文件的布局中给需要显示搜索按钮的控件(如EditText)加上
android:imeOptions="actionSearch"
android:singleLine="true"
如果android:singleLine="true"过时,可以替换为
android:imeOptions="actionSearch"
android:inputType="text"
二.给搜索按钮设置监听
et_search.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH){
// 先隐藏键盘
((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE))
.hideSoftInputFromWindow(SearchEnterprise.this
.getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
//接下来写点击搜索按钮之后的逻辑
return true;
}
return false;
}
});