之前遇到的問題沒來得及記錄下來,趁今晚有空就重新回憶並寫下了。
我們在用到EditText這個空間時經常需要重寫軟鍵盤中的回車事件以配合我們接下來的響應,比如點擊回車變成搜索、發送、完成等。
EditText為我們提供了一個屬性imeOptions用來替換軟鍵盤中enter鍵的外觀,如actionDone會使外觀變成“完成”。
下面列出比較經常用到的幾個屬性以及替換的文本外觀:
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
設置的方法可以在布局文件中設置 android:imeOptions="actionNext" 或者在代碼中 mUserEdit.setImeOptions(EditorInfo.IME_ACTION_NEXT);
接下來就需要重寫回車事件了,通過setOnEditorActionListener
private void initListener() { mUserEdit .setOnEditorActionListener(new TextView.OnEditorActionListener() { public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {if (actionId == EditorInfo.IME_ACTION_SEND || (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
//讓mPasswordEdit獲取輸入焦點 mPasswordEdit.requestFocus(); return true; } return false; } }); }
到此重寫回車事件就完成了。
下面順便列出幾個edittext常用的屬性:
android:password="true" 這條可以讓EditText顯示的內容自動為星號,輸入時內容會在1秒內變成*字樣。
android:numeric="true" 這條可以讓輸入法自動變為數字輸入鍵盤,同時僅允許0-9的數字輸入
android:capitalize="abcde" 這樣僅允許接受輸入abcde,一般用於密碼驗證
android:hint="密碼" 設置顯示的提示信息
android:singleLine="true" 設置單行輸入,這樣就不會自動換行
