在手機彈出的軟鍵盤中,回車鍵變?yōu)樗阉麈I
1、修改EditText屬性:
<EditText
android:id="@+id/et_search"
android:layout_width="100dp"
android:layout_height="25dp"
android:textSize="12sp"
android:hint="請輸入關(guān)鍵詞"
android:imeOptions="actionSearch"
android:singleLine="true"/>
android:imeOption="actionSearch"的作用是將回車兩字改為搜索,
android:singleLine="true"的作用是防止搜索框換行。
2、 點擊時執(zhí)行兩次監(jiān)聽事件的問題:
每次點擊軟鍵盤的搜索鍵都會執(zhí)行兩次搜索方法,
沒有加event.getAction() == KeyEvent.ACTION_DOWN這句判斷。
修改代碼如下:
OnKeyListener事件:
et_search=(EditText)findViewById(R.id.et_search);
et_search.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
//是否是回車鍵
if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
//隱藏鍵盤
((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE))
.hideSoftInputFromWindow(SearchActivity.this.getCurrentFocus()
.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
//搜索
search();
}
return false;
}
});