首先在布局文件中添加下面这两行
然后在java代码中添加输入框的监听
//设置输入框监听
film_ed_search.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
//注意:搜狗输入法需要多加后面一层判断EditorInfo.IME_ACTION_UNSPECIFIED
if (actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_UNSPECIFIED) {//搜索按键
((InputMethodManager) film_ed_search.getContext()
.getSystemService(Context.INPUT_METHOD_SERVICE))
.hideSoftInputFromWindow(getActivity().getCurrentFocus()
.getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
//实现自己的搜索逻辑
String searchKey = film_ed_search.getText().toString().trim();
if (TextUtils.isEmpty(searchKey)) {
showToast("输入内容不能为空哦");
return false;
}
//跳转
Intent intent = new Intent(getActivity(), SearchCinemaActivity.class);
intent.putExtra("searchCinema", film_ed_search.getText().toString());
startActivity(intent);
return true;
}
return false;
}
});
效果如下