Original address: http://blog.csdn.net/zhufuing/article/details/18964725
Objective:
We sometimes encounter on the Android phone when listening to soft keyboard keys, such as: We can click on the browser to enter the URL after the "GO" button in the lower right corner of the soft keyboard to load the URL page, click on the Search box, the bottom right corner of the search symbol can be searched , or after all data has been entered, click "Done" in the lower right corner to proceed immediately.
:
Function 1: Rewrite the activity of the Dispatchkeyevent (KeyEvent event) method, in which the Listener Keyeventkey.keycode_enter key (the bottom right corner of the OK key), when this key is pressed, Hides the IME soft keyboard, sets EditText content, and loads WebView content.
[Java]View Plaincopy
- @Override
- Public Boolean dispatchkeyevent (KeyEvent event) {
- if (event.getkeycode () = = Keyevent.keycode_enter) {
- / * Hide 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 (KeyEvent event) feels a bit sledgehammer, because we are very likely to do other tasks in this method, so we can use the Onkeylistener method to listen for the soft keyboard keys.
[Java]View Plaincopy
- 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) {
- / * Hide 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;
- }
- };
[Java]View Plaincopy
- Edittext.setonkeylistener (Onkeylistener);
Function 3:
The third method I think can help programmers more accurately judge the lower right corner of the button, in order to deal with more complex situations. It helps programmers make more granular actions based on the "GO", "Done", "search" keys under the current message.
[Java]View Plaincopy
- Edittext.setoneditoractionlistener (new Textview.oneditoractionlistener () {
- @Override
- Public Boolean oneditoraction (TextView V, int ActionId, keyevent event) {
- / * Determine if the "GO" key is * /
- if (ActionId = = Editorinfo.ime_action_go) {
- / * Hide Soft keyboard * /
- 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 soft keyboard to the lower right corner to determine the key style: Soft keyboard Input method is not a constant, such as its lower right corner of the "OK" button, when the search box will become a search icon with the button, in the browser address bar will become "GO" key, When we write the app, we may also set the input method's "OK" key depending on the situation, and change the method is to set the Imeoptions property of the EditText control to a different value (the ENTER key can display different text and patterns).
[HTML]View Plaincopy
- <EditText
- android:id="@+id/edittext"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:singleline="true"
- android:imeoptions="Actionsearch"/>
Actionnone: Enter, press the cursor to the next line
Actiongo:go,
Actionsearch: Magnifying Glass
Actionsend:send
Actionnext:next
Actiondone:done, OK/finish, hide soft keyboard, even if not the last text input box digression:
When I was writing this demo, I found a problem with webview, that the direct use of the Webview.load (URL) method would pop up the system browser on the phone to access the URL link instead of the webview we set up. The solution I found was to use Webview.setwebviewclient (...). method to ensure that the URL is loaded on the webview of the activity.
demo:http://download.csdn.net/detail/zhufuing/6903671