今天測試提出這個問題,就是輸入搜索信息點後點擊鍵盤上面的搜索按鈕不能進行搜索,因爲了解過自定義鍵盤,所以我第一想到的就是自定義一個鍵盤,不過怎麼想也是有點麻煩,後來問了一下同事,說是可以直接監聽現有的鍵盤的搜索事件,在此做一下記錄
有時候爲了佈局美觀,在搜索時沒有搜索按鈕,而是調用軟件盤上的按鈕。調用的實現只需要在XML在輸入框中加入android:imeOptions="actionSearch",調用軟鍵盤時,回車鍵就會顯示搜索二字。然後調用 OnEditorActionListener
searchText.setOnEditorActionListener(
new
OnEditorActionListener() {
@Override
public
boolean
onEditorAction(TextView v,
int
actionId, KeyEvent event) {
if
(actionId ==EditorInfo.IME_ACTION_SEARCH){
// 先隱藏鍵盤
((InputMethodManager) searchText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE))
.hideSoftInputFromWindow(
getActivity()
.getCurrentFocus()
.getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
return
true
;
}
return
false
;
}
});