整理自: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)按鍵訊息:
- editText.setOnEditorActionListener(new EditText.OnEditorActionListener() {
-
- @Override
- public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
- if (actionId == EditorInfo.IME_ACTION_DONE) {
- InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
- imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
-
- doSomething();
-
- return true;
- }
- return false;
- }
-
- });
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。