為簡化搜索欄闰蚕,很多APP取消了搜索鍵,將軟鍵盤的回車鍵改為搜索鍵實現(xiàn)搜索,以下是實現(xiàn)步驟:
一敦冬、在作為搜索欄的EditText里添加兩個屬性:
android:singleLine="true"
android:imeOptions="actionSearch"
二键思、設置EditText的監(jiān)聽事件
edt_search.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
// 先隱藏鍵盤
((InputMethodManager) MyApplicant.getContext()
.getSystemService(Context.INPUT_METHOD_SERVICE))
.hideSoftInputFromWindow(SearchActivity.this
.getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
if (edt_search.getText().toString().isEmpty()) {
ToastShort("搜索欄不能為空梦染!");
} else {
//搜索
doSearch();
}
return true;
}
return false;
}
});