主要用到的是EditText的一个内部属性:android:imeOptions , 不同属性值对应的含义如下
1.actionUnspecified 未指定,对应常量EditorInfo.IME_ACTION_UNSPECIFIED.
2.actionNone 没有动作,对应常量EditorInfo.IME_ACTION_NONE
3.actionGo 去往,对应常量EditorInfo.IME_ACTION_GO
4.actionSearch 搜索,对应常量EditorInfo.IME_ACTION_SEARCH
5.actionSend 发送,对应常量EditorInfo.IME_ACTION_SEND
6.actionNext 下一个,对应常量EditorInfo.IME_ACTION_NEXT
7.actionDone 完成,对应常量EditorInfo.IME_ACTION_DONE
如果要显示为搜索的话,你可以下面这么做:
- 在布局中的EditText添加属性:
android:imeOptions="actionSearch"
- 在代码中设置监听事件
//键盘中显示搜索按钮
etKeyWord.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH || actionId == KeyEvent.KEYCODE_ENTER) {
//先隐藏键盘
((InputMethodManager) etKeyWord.getContext().getSystemService(Context.INPUT_METHOD_SERVICE))
.hideSoftInputFromWindow(
InputTagActivity.this
.getCurrentFocus()
.getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
//实现搜索逻辑
gotoSearch();
return true;
}
return false;
}
});