步骤如下:
1、修改Editview属性:android:imeOptions="actionSearch" 在该Editview获得焦点的时候将“回车”键改为“搜索”
android:singleLine="true" 不然回车【搜索】会换行
2.
@Override
public
boolean
onEditorAction
(
TextView
v
,
int
actionId
,
KeyEvent
event
)
{
// 输入法中点击搜索
if
(
actionId
==
EditorInfo
.
IME_ACTION_SEARCH
)
{
//这里调用搜索方法
return
true
;
}
return
false
;
}
}
)
;
对于搜狗输入法~actionId如下~
|
//一般输入法或搜狗输入法点击搜索按键
if
(
actionId
==
EditorInfo
.
IME_ACTION_SEARCH
||
actionId
==
EditorInfo
.
IME_ACTION_UNSPECIFIED
)
{
//这里调用搜索方法
return
true
;
}
if (actionId == EditorInfo.IME_ACTION_SEARCH) { return true; } else if (actionId == EditorInfo.IME_ACTION_UNSPECIFIED && event != null) { if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_ENTER) { return true; } } |
3
4
5
|
//一般输入法或搜狗输入法点击搜索按键
if (actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_UNSPECIFIED) {
//这里调用搜索方法
return true;
}
项目中在SearchBar的布局文件里面确实设置了EditView的属性为actionSearch
|