- 在 xml中:
editText中 添加以下属性
android:imeOptions="actionSearch"
- 在逻辑里处理
@BindView(R.id.address_edit)
EditText addressEdit;
addressEdit.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
if (i==IME_ACTION_SEARCH){
String content=addressEdit.getText().toString();
if (TextUtils.isEmpty(content)){
return true;
}
//进行搜索的逻辑处理
return true;
}
return false;
}
});
ps:
- imeOptions属性: android:imeOptions属性详解(Enter功能)
(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效果: