手機軟鍵盤按鍵監聽

轉自:http://blog.csdn.net/zhufuing/article/details/18964725/

前言:


我們在Android手機上面有時候會遇到監聽手機軟鍵盤按鍵的時候,例如:我們在瀏覽器輸入url完畢後可以點擊軟鍵盤右下角的“Go”按鍵加載url頁面;在點擊搜索框的時候,點擊右下角的search符號鍵可以進行搜索;或者在全部數據輸入完畢後,點擊右下角的"done"就馬上進行下一步操作。

效果圖:
























function 1:


重寫Activity的dispatchKeyEvent(KeyEvent event)方法,在其中監聽KeyEventKey.KEYCODE_ENTER鍵(右下角確定鍵),當此鍵按下的時候,隱藏輸入法軟鍵盤,設置edittext內容和加載webview內容。
[java] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. @Override  
  2.     public boolean dispatchKeyEvent(KeyEvent event) {  
  3.         if(event.getKeyCode() == KeyEvent.KEYCODE_ENTER){  
  4.             /*隱藏軟鍵盤*/  
  5.             InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);  
  6.             if(inputMethodManager.isActive()){  
  7.                 inputMethodManager.hideSoftInputFromWindow(MainActivity.this.getCurrentFocus().getWindowToken(), 0);  
  8.             }  
  9.               
  10.             edittext.setText("success");  
  11.             webview.loadUrl(URL);  
  12.             return true;  
  13.         }  
  14.         return super.dispatchKeyEvent(event);  
  15.     }  

function 2:


重寫dispatchKeyEvent(KeyEvent event)的方法感覺有點用牛刀的感覺,因爲我們非常可能在這個方法中進行其他任務,所以我們可以使用OnKeyListener的方法來監聽軟鍵盤按鍵。
[java] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. private OnKeyListener onKeyListener = new OnKeyListener() {  
  2.           
  3.         @Override  
  4.         public boolean onKey(View v, int keyCode, KeyEvent event) {  
  5.             if(keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN){  
  6.                 /*隱藏軟鍵盤*/  
  7.                 InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);  
  8.                 if(inputMethodManager.isActive()){  
  9.                     inputMethodManager.hideSoftInputFromWindow(v.getApplicationWindowToken(), 0);  
  10.                 }  
  11.                   
  12.                 edittext.setText("success");  
  13.                 webview.loadUrl(URL);  
  14.                   
  15.                 return true;  
  16.             }  
  17.             return false;  
  18.         }  
  19.     };  
[java] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. edittext.setOnKeyListener(onKeyListener);  

function 3:


第三種方法我認爲可以幫助程序員更精確的判斷右下角按鍵情況,以便應對更加複雜的情況。它可以幫助程序員依據當前郵件下爲“GO”,“done”,“search”鍵的情況下做出更細分的操作。

[java] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. edittext.setOnEditorActionListener(new TextView.OnEditorActionListener() {  
  2.               
  3.             @Override  
  4.             public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {  
  5.                 /*判斷是否是“GO”鍵*/  
  6.                 if(actionId == EditorInfo.IME_ACTION_GO){  
  7.                     /*隱藏軟鍵盤*/  
  8.                     InputMethodManager imm = (InputMethodManager) v  
  9.                             .getContext().getSystemService(  
  10.                                     Context.INPUT_METHOD_SERVICE);  
  11.                     if (imm.isActive()) {  
  12.                         imm.hideSoftInputFromWindow(  
  13.                                 v.getApplicationWindowToken(), 0);  
  14.                     }  
  15.                       
  16.                     edittext.setText("success");  
  17.                     webview.loadUrl(URL);  
  18.                       
  19.                     return true;  
  20.                 }  
  21.                 return false;  
  22.             }  
  23.         });  

改變軟鍵盤右下角確定鍵樣式:


軟鍵盤輸入法的按鍵並不是一成不變的,例如它的右下角的“確定”鍵,在有搜索框的時候就會變成帶搜索圖標的按鍵,在瀏覽器地址欄的時候則會變成“GO”鍵,我們在寫App的時候也可能根據情況的不同設置輸入法的“確定”鍵,改變方法就是給EditText控件的imeOptions屬性設置成不同的值(此時Enter鍵可以顯示不同的文字和圖案)。
[html] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. <EditText  
  2.         android:id="@+id/edittext"  
  3.         android:layout_width="match_parent"  
  4.         android:layout_height="wrap_content"  
  5.         android:singleLine="true"  
  6.         android:imeOptions="actionSearch"/>  
actionNone : 回車鍵,按下後光標到下一行
actionGo : Go,
actionSearch : 放大鏡
actionSend : Send
actionNext : Next
actionDone : Done,確定/完成,隱藏軟鍵盤,即使不是最後一個文本輸入框

題外話:


       我在寫這個demo的時候,發現了webview的一個問題,就是直接使用webview.load(url)方法會在手機上面彈出系統瀏覽器來訪問url鏈接,而不是我們設置的webview,我找到的解決辦法就是使用webview.setWebViewClient(....)的方法來確保url會在activity的webview上面加載。


demo下載地址:





轉自:http://blog.csdn.net/lastdream/article/details/24365633

雖然通常輸入法軟鍵盤右下角會是回車按鍵

但我們經常會看到點擊不同的編輯框,輸入法軟鍵盤右下角會有不同的圖標

點擊瀏覽器網址欄的時候,輸入法軟鍵盤右下角會變成“Go”或“前往”

而我們點擊Google搜索框,輸入法軟鍵盤右下角會變成 放大鏡 或者“搜索”

而決定這個圖標的變換的參數就是EditText中的 Android:imeOptions

android:imeOptions的值有actionGo、 actionSend 、actionSearch、actionDone等,這些意思都很明顯


[html] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. <EditText  
  2.       android:id="@+id/editText"  
  3.       android:layout_width="200dp"  
  4.       android:layout_height="wrap_content"  
  5.       android:imeOptions="actionSearch"  
  6.    />  



而其在Java代碼中對應的值爲EditorInfo.IME_ACTION_XXX 

在代碼中通過editText.setOnEditorActionListener方法添加相應的監聽,因爲有些action是需要在代碼中添加具體的相關操作的


[java] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. EditText editText = (EditText) contentView.findViewById(R.id.editText);  
  2.         editText.setOnEditorActionListener(new OnEditorActionListener() {  
  3.             @Override  
  4.             public boolean onEditorAction(TextView v, int actionId,  
  5.                     KeyEvent event) {  
  6.                 if (actionId == EditorInfo.IME_ACTION_SEARCH) {  
  7.                     Toast.makeText(getActivity(), "1111111",Toast.LENGTH_SHORT).show();  
  8.                 }  
  9.   
  10.                 return false;  
  11.             }  
  12.         });  

然而當我們設置這一切後,卻發現點擊輸入框,輸入法鍵盤完全沒變化,還是回車鍵

這並不是上面的屬性和方法無效,而是我們還需要設置別的屬性來使它們生效

經過試驗 設置下面兩個屬性中的一個即可使這個屬性生效(應該還有其他的屬性也可以,沒去試驗)

1 將singleLine設置爲true

2 將inputType設置爲text 

[html] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. <EditText  
  2.       android:id="@+id/editText"  
  3.       android:layout_width="200dp"  
  4.       android:layout_height="wrap_content"  
  5.       android:imeOptions="actionSearch"  
  6.       android:singleLine="true"  
  7.       android:inputType="text"  
  8.    />  


java代碼設置

[java] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. editText.setImeOptions(EditorInfo.IME_ACTION_SEARCH);  
  2. editText.setInputType(EditorInfo.TYPE_CLASS_TEXT);  
  3. editText.setSingleLine(true);  




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章