Android App監聽軟鍵盤按鍵的三種方式

來源:互聯網
上載者:User

前言:

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

:






















function 1:
重寫Activity的dispatchKeyEvent(KeyEvent event)方法,在其中監聽KeyEventKey.KEYCODE_ENTER鍵(右下角確定鍵),當此鍵按下的時候,隱藏IME軟鍵盤,設定edittext內容和載入webview內容。
@Overridepublic boolean dispatchKeyEvent(KeyEvent event) {if(event.getKeyCode() == KeyEvent.KEYCODE_ENTER){/*隱藏軟鍵盤*/InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);if(inputMethodManager.isActive()){inputMethodManager.hideSoftInputFromWindow(MainActivity.this.getCurrentFocus().getWindowToken(), 0);}edittext.setText("success");webview.loadUrl(URL);return true;}return super.dispatchKeyEvent(event);}
function 2:
重寫dispatchKeyEvent(KeyEvent event)的方法感覺有點用牛刀的感覺,因為我們非常可能在這個方法中進行其他任務,所以我們可以使用OnKeyListener的方法來監聽軟鍵盤按鍵。
private OnKeyListener onKeyListener = new OnKeyListener() {@Overridepublic boolean onKey(View v, int keyCode, KeyEvent event) {if(keyCode == KeyEvent.KEYCODE_ENTER){/*隱藏軟鍵盤*/InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);if(inputMethodManager.isActive()){inputMethodManager.hideSoftInputFromWindow(v.getApplicationWindowToken(), 0);}edittext.setText("success");webview.loadUrl(URL);return true;}return false;}};
edittext.setOnKeyListener(onKeyListener);
function 3:

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

edittext.setOnEditorActionListener(new TextView.OnEditorActionListener() {@Overridepublic boolean onEditorAction(TextView v, int actionId, KeyEvent event) {/*判斷是否是“GO”鍵*/if(actionId == EditorInfo.IME_ACTION_GO){/*隱藏軟鍵盤*/InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);if (imm.isActive()) {imm.hideSoftInputFromWindow(v.getApplicationWindowToken(), 0);}edittext.setText("success");webview.loadUrl(URL);return true;}return false;}});

改變軟鍵盤右下角確定鍵樣式:
軟鍵盤IME的按鍵並不是一成不變的,例如它的右下角的“確定”鍵,在有搜尋方塊的時候就會變成帶搜尋表徵圖的按鍵,在瀏覽器地址欄的時候則會變成“GO”鍵,我們在寫App的時候也可能根據情況的不同設定IME的“確定”鍵,改變方法就是給EditText控制項的imeOptions屬性設定成不同的值(此時Enter鍵可以顯示不同的文字和圖案)。
<EditText        android:id="@+id/edittext"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:singleLine="true"        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://download.csdn.net/detail/zhufuing/6903671

聯繫我們

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