Preface:
On android phones, we sometimes encounter keyboard buttons when listening to mobile phones. For example, after entering a url in the browser, We can click the "GO" button in the lower-right corner of the keyboard to load the url page; when you click the search box, click the search symbol key in the lower-right corner to search. Or, after all the data is input, click "done" in the lower-right corner to perform the next step.
:
Function 1:
Override the dispatchKeyEvent (KeyEvent event) method of the Activity and listen to KeyEventKey. KEYCODE_ENTER (the key in the lower right corner). When this key is pressed, the soft keyboard of the input method is hidden, edittext content is set, and webview content is loaded.
@ Overridepublic boolean dispatchKeyEvent (KeyEvent event) {if (event. getKeyCode () = KeyEvent. KEYCODE_ENTER) {/* Hide the soft keyboard */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:
The method of rewriting dispatchKeyEvent is a bit of a cool, because we are very likely to perform other tasks in this method, so we can use the OnKeyListener method to listen to the soft keyboard keys.
Private OnKeyListener onKeyListener = new OnKeyListener () {@ Overridepublic boolean onKey (View v, int keyCode, KeyEvent event) {if (keyCode = KeyEvent. KEYCODE_ENTER) {/* Hide the soft keyboard */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:
In my opinion, the third method can help programmers determine the buttons in the lower right corner more accurately to cope with more complex situations. It helps programmers perform more detailed operations based on the keys "GO", "done", and "search" in the current email.
Edittext. setoneditexceptionlistener (new TextView. oneditexceptionlistener () {@ Overridepublic boolean oneditexception (TextView v, int actionId, KeyEvent event) {/* determine whether it is a "GO" key */if (actionId = EditorInfo. IME_ACTION_GO) {/* Hide the keypad */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 ;}});
Change the key style at the bottom right corner of the keyboard:
The keys of the soft keyboard input method are not static. For example, the "OK" key in the lower right corner of the Soft Keyboard will change to a button with a search icon when there is a search box, in the address bar of the browser, the "GO" Key is changed. When writing an App, we may also set the "OK" key for the input method based on different situations, the change method is to set the imeOptions attribute of the EditText control to different values (at this time, the Enter key can display different texts and patterns ).
<EditText android:id="@+id/edittext" android:layout_width="match_parent" android:layout_height="wrap_content" android:singleLine="true" android:imeOptions="actionSearch"/>
ActionNone: Enter key. Press the button and move the cursor to the next line.
ActionGo: Go,
ActionSearch: magnifiers
ActionSend: Send
ActionNext: Next
ActionDone: Done, OK/complete, hide the keyboard, even if it is not the last text input box:
When I wrote this demo, I found a problem with webview, that is, using webview directly. the load (url) method will pop up the system browser on the mobile phone to access the url link, instead of the webview we set. The solution I found is to use webview. setWebViewClient (....) to ensure that the url is loaded on the webview of the activity.
Demo:
Http://download.csdn.net/detail/zhufuing/6903671