在EditText中開啟軟鍵盤的”Done”按鈕

來源:互聯網
上載者:User

整理自:http://hi.baidu.com/doyee/blog/item/e2a8481628ebed4521a4e948.html

http://groups.google.com/group/android-developers/browse_thread/thread/fe95e6e838ee48b1?pli=1在EditText中,可以使用setImeOptions()方法來來開啟軟鍵盤的"Done"按鈕。範例程式碼如下:editText.setImeOptions(EditorInfo.IME_ACTION_DONE);介面如下:

 按下"Done"按鈕的預設行為是關閉軟鍵盤,但是我們可以通過EditText的setOnEditorActionListener()方法來設定OnEditorActionListener以便添加自己的行為.捕獲Android文本輸入框的軟鍵盤完成(Done)按鍵訊息:

  1. editText.setOnEditorActionListener(new EditText.OnEditorActionListener() {  
  2.   
  3.     @Override  
  4.     public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {  
  5.         if (actionId == EditorInfo.IME_ACTION_DONE) {  
  6.             InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);  
  7.             imm.hideSoftInputFromWindow(v.getWindowToken(), 0);  
  8.               
  9.             doSomething();  
  10.               
  11.             return true;    
  12.         }  
  13.         return false;  
  14.     }  
  15.       
  16. }); 
EditorInfo.IME_ACTION_DONE可以和其他的標誌一起組合使用來設定軟鍵盤,比如:editText.setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI|EditorInfo.IME_ACTION_DONE); 注意1:EditorInfo.IME_ACTION_DONE只有對android:singleLine="true"的EditText有效。至少對HTC_A9191是這樣的。注意2:對於EditorInfo.IME_ACTION_DONE,有些IME並不支援它,比如搜狐拼音IME。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.