標籤:
EditText通過設定android:imeOptions來改變預設的文本或者樣式。這裡舉幾個常用的常量值:
actionUnspecified 未指定,對應常量EditorInfo.IME_ACTION_UNSPECIFIED.
actionNone 沒有動作,對應常量EditorInfo.IME_ACTION_NONE
actionGo 去往,對應常量EditorInfo.IME_ACTION_GO
actionSearch 搜尋,對應常量EditorInfo.IME_ACTION_SEARCH
actionSend 發送,對應常量EditorInfo.IME_ACTION_SEND
actionNext 下一個,對應常量EditorInfo.IME_ACTION_NEXT
actionDone 完成,對應常量EditorInfo.IME_ACTION_DONE
首先:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="50dp" android:orientation="horizontal"> <EditText android:id="@+id/searchHeaderEdit" android:layout_width="wrap_content" android:layout_height="match_parent" android:drawableRight="@mipmap/icon_search" android:imeOptions="actionSearch" android:minWidth="300dp" android:paddingLeft="10dp" android:singleLine="true" android:textColor="@android:color/white" android:textSize="18sp" /></LinearLayout>
其中:
android:singleLine="true"
這行是必須的,如果不設定單行,設定的imeOptions不會生效的。
然後通過:
mSearchEdit.setOnEditorActionListener(new TextView.OnEditorActionListener() { @Overridepublic boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if (actionId == EditorInfo.IME_ACTION_SEARCH ||(event!=null&&event.getKeyCode()== KeyEvent.KEYCODE_ENTER)){ if (TextUtils.isEmpty(mSearchEdit.getText())){ mSearchEdit.requestFocus(); mSearchEdit.setError("不可為空"); }else { //do reseach } return true; } return false;} });
效果如下:
Android 更改鍵盤Enter鍵文字