Android app three ways to monitor soft keyboard keys

Source: Internet
Author: User

Preface:

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 key), when this key is pressed, hide the input method soft keyboard, Set edittext content and load webview content.

@Override PublicBoolean dispatchkeyevent (KeyEventEvent) {        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; }        returnSuper.dispatchkeyevent (Event); }
function 2:

The way to rewrite 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.

PrivateOnkeylistener Onkeylistener =NewOnkeylistener () {@Override PublicBoolean OnKey (View V,intKeyCode, KeyEventEvent) {            if(KeyCode = =keyevent.keycode_enter) {                /*Hide Soft Keyboard*/Inputmethodmanager Inputmethodmanager=(Inputmethodmanager) Getsystemservice (Context.input_method_service); if(Inputmethodmanager.isactive ()) {Inputmethodmanager.hidesoftinputfromwindow (v.getapplicationwindowt Oken (),0); } edittext.settext ("Success");                                 Webview.loadurl (URL); return true; }            return false; }    };
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.

Edittext.setoneditoractionlistener (NewTextview.oneditoractionlistener () {@Override PublicBoolean oneditoraction (TextView V,intActionId, KeyEventEvent) {                /*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.getapplicatio Nwindowtoken (),0); } edittext.settext ("Success");                                         Webview.loadurl (URL); return true; }                return false; }        });
change the bottom right corner of the soft keyboard to determine the key style:

Soft keyboard Input method of the button is not constant, such as its lower right corner of the "OK" key, when there is a search box will become the button with the search icon, in the browser address bar will become the "GO" key, we write the app may also be different depending on the situation to set the input method "OK" key, The way to change this is to set the Imeoptions property of the EditText control to a different value (the ENTER key can display different text and patterns).

<edittext android:id="@+id/edittext" android:layout_width="Match_ Parent" android:layout_height="wrap_content" android:singleline="  true" android:imeoptions="actionsearch"></ Edittext>

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

Off topic:

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

Android app three ways to monitor soft keyboard keys

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.