轉自: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內容。
-
@Override
-
public 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() {
-
-
@Override
-
public boolean onKey(View v, int keyCode, KeyEvent event) {
-
if(keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN){
-
-
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() {
-
-
@Override
-
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
-
-
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;
-
}
-
});
改變軟鍵盤右下角確定鍵樣式:
軟鍵盤輸入法的按鍵並不是一成不變的,例如它的右下角的“確定”鍵,在有搜索框的時候就會變成帶搜索圖標的按鍵,在瀏覽器地址欄的時候則會變成“GO”鍵,我們在寫App的時候也可能根據情況的不同設置輸入法的“確定”鍵,改變方法就是給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://blog.csdn.net/lastdream/article/details/24365633
雖然通常輸入法軟鍵盤右下角會是回車按鍵
但我們經常會看到點擊不同的編輯框,輸入法軟鍵盤右下角會有不同的圖標
點擊瀏覽器網址欄的時候,輸入法軟鍵盤右下角會變成“Go”或“前往”
而我們點擊Google搜索框,輸入法軟鍵盤右下角會變成 放大鏡 或者“搜索”
而決定這個圖標的變換的參數就是EditText中的 Android:imeOptions
android:imeOptions的值有actionGo、 actionSend 、actionSearch、actionDone等,這些意思都很明顯
-
<EditText
-
android:id="@+id/editText"
-
android:layout_width="200dp"
-
android:layout_height="wrap_content"
-
android:imeOptions="actionSearch"
-
/>
而其在Java代碼中對應的值爲EditorInfo.IME_ACTION_XXX
在代碼中通過editText.setOnEditorActionListener方法添加相應的監聽,因爲有些action是需要在代碼中添加具體的相關操作的
-
EditText editText = (EditText) contentView.findViewById(R.id.editText);
-
editText.setOnEditorActionListener(new OnEditorActionListener() {
-
@Override
-
public boolean onEditorAction(TextView v, int actionId,
-
KeyEvent event) {
-
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
-
Toast.makeText(getActivity(), "1111111",Toast.LENGTH_SHORT).show();
-
}
-
-
return false;
-
}
-
});
然而當我們設置這一切後,卻發現點擊輸入框,輸入法鍵盤完全沒變化,還是回車鍵
這並不是上面的屬性和方法無效,而是我們還需要設置別的屬性來使它們生效
經過試驗 設置下面兩個屬性中的一個即可使這個屬性生效(應該還有其他的屬性也可以,沒去試驗)
1 將singleLine設置爲true
2 將inputType設置爲text
-
<EditText
-
android:id="@+id/editText"
-
android:layout_width="200dp"
-
android:layout_height="wrap_content"
-
android:imeOptions="actionSearch"
-
android:singleLine="true"
-
android:inputType="text"
-
/>
java代碼設置
-
editText.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
-
editText.setInputType(EditorInfo.TYPE_CLASS_TEXT);
-
editText.setSingleLine(true);