第一种方法
1.EditText控件加上
android:imeOptions="actionSearch"
android:singleLine="true"
2.
//键盘的监听
ed.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
//发现执行了两次因为onkey事件包含了down和up事件,所以只需要加入其中一个即可。
if (keyCode == KeyEvent.KEYCODE_ENTER&& event.getAction() == KeyEvent.ACTION_DOWN) {//修改回车键功能
// 先隐藏键盘
((InputMethodManager) getActivity().getSystemService(INPUT_METHOD_SERVICE)).hideSoftInputFromWindow
(getActivity().getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
event(); //搜索触发的事件
}
return false;
}
});
第二种方法
1.EditText控件加上
android:imeOptions="actionSearch"
android:singleLine="true"
2.绑定
ed.setOnEditorActionListener(this);
3.类实现这个接口
implements TextView.OnEditorActionListener
4.实现方法
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
event();//搜索触发的事件
return true;
}
return false;
}
弹出键盘
private void showInputMethod(Context context) {
//自动弹出键盘
InputMethodManager inputManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
//强制隐藏Android输入法窗口
//inputManager.hideSoftInputFromWindow(edit.getWindowToken(),0);
}