Android實習劄記(1)---EditText屬性之ImeOption詳解,androidedittext

來源:互聯網
上載者:User

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下載:

回去再給出來吧~~

如下:







聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.