Android實習劄記(1)---EditText屬性之ImeOption詳解,androidedittext
Android實習劄記(1)---EditText屬性之ImeOption詳解
1)這個屬性拿來幹嘛的?
答:當我們往EditText等可以輸入文字的UI控制項的地方輸入時,彈出的小鍵盤的
右下角的Enter鍵!你沒聽錯,這個屬性就是關於Enter鍵的!!!
我們可以通過ImeOption屬性來設定enter鍵顯示的內容,以及進行事件監聽~
2)如何修改Enter鍵顯示的內容:
答:直接設定ImeOption屬性即可,另外,使用第三方IME可能出現不同的結果,
或者沒變化,這裡只以搜狗IME為參考,其他的就不理了!
都是只需要往EditText中添加ImeOption屬性而已!另外如果需要在Java代碼中設定的話,調用
setImeOption方法,然後參數actionSearch只需要改成:EditorInfo.IME_ACTION_SEARCH即可
以此類推
<EditText android:id="@+id/editsearch" android:layout_width="match_parent" android:layout_height="wrap_content" android:imeOptions="actionSearch" android:text="actionSearch,查詢" android:singleLine="true" />
測試後的如下:
①原裝IME/搜狗IME
actionUnspecified:~~~
actionNone:~~~
actionGo:~~~
actionSearch:~~~
actionSend:~~~
actionNext:~~~
actionDone:~~~
!!!另外,還有一個問題就是,如果你的系統版本是高於2.3的話,有可能這個屬性設定了,但是沒有效果;
只需要我們給EditText設定singleLine為true或者inputType為text即可!!!
3)捕獲Enter鍵的點擊事件
這個也很簡單,只需為EditText設定事件監聽:setOnEditorActionListener即可,然後
重寫裡面的onEditorAction(TextView v, int actionId, KeyEvent event)方法即可,
接著就根據你的需求,對應Enter鍵處於什麼狀態,就響應對應事件咯,很簡單,範例程式碼如下:
注意是按下Enter鍵才會觸發這個事件哦!
public OnEditorActionListener editorActionListener = new OnEditorActionListener() {@Overridepublic boolean onEditorAction(TextView v, int actionId, KeyEvent event) {if(actionId == EditorInfo.IME_ACTION_UNSPECIFIED){Toast.makeText(getApplicationContext(), "無效果", Toast.LENGTH_SHORT).show();}else if(actionId == EditorInfo.IME_ACTION_SEARCH){Toast.makeText(getApplicationContext(), "查詢", Toast.LENGTH_SHORT).show();}return false;}};
4)測試demo下載:
回去再給出來吧~~
如下: