Android Click EditText Anywhere outside of the text box to hide the keyboard solution

Source: Internet
Author: User
Tags gety

1, implement method one: By setting the Click event to the parent layout file of the current interface (equivalent to setting a click event for the entire activity), the keyboard is hidden in the event

1 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"2 Android:id= "@+id/traceroute_rootview"3 Android:layout_width= "Fill_parent"4 Android:layout_height= "Fill_parent"5 Android:background= "@color/white"6 android:clickable= "true"7 android:gravity= "Center_horizontal"8 android:orientation= "vertical" >9 Ten </LinearLayout>

Plus ID and clickable=true

Then, in OnCreate, add the listener for the onclick event:

Findviewbyid (R.id.traceroute_rootview). Setonclicklistener (this);  

In the onclick:

    @Override    publicvoid  OnClick (View v) {        switch  ( V.getid ()) {        case  r.id.traceroute_rootview:             = (Inputmethodmanager)             Getsystemservice (Context.input_method_service);              0);              Break ;        }    }

This will perfectly solve the hidden effect outside the input box, which can be used if the layout is not particularly complex or if there are few other touch events.

2, realize the idea of two: by Dispatchtouchevent each Action_down event to dynamically determine the non-edittext itself region of the Click event, and then block in the event.

@Override Public Booleandispatchtouchevent (motionevent ev) {if(ev.getaction () = =Motionevent.action_down) {View v=Getcurrentfocus (); if(Isshouldhideinput (v, Ev)) {Inputmethodmanager IMM=(Inputmethodmanager) Getsystemservice (Context.input_method_service); if(IMM! =NULL) {Imm.hidesoftinputfromwindow (V.getwindowtoken (),0); }            }            return Super. dispatchtouchevent (EV); }        //necessary, otherwise all the components will not be touchevent.        if(GetWindow (). superdispatchtouchevent (EV)) {return true; }        returnontouchevent (EV); }

Isshoudhideinput (View v,motionevent e) Method:

     Public  BooleanIsshouldhideinput (View V, motionevent event) {if(V! =NULL&& (vinstanceofEditText)) {            int[] Lefttop = {0, 0 }; //get the current location of the input boxV.getlocationinwindow (lefttop); intleft = Lefttop[0]; inttop = lefttop[1]; intBottom = top +v.getheight (); intright = left +v.getwidth (); if(Event.getx () > Left && event.getx () < Right&& event.gety () > Top && event.gety () <bottom) {                //Click on the input box area to retain the Click EditText event                return false; } Else {                return true; }        }        return false; }

This approach is cumbersome to implement, and the solution is similar to the event distribution mechanism in iOS, which is clearer for handling hidden events, distributed through layers of events, and then judged if the area needs to be masked.

Android Click EditText Anywhere outside of the text box to hide the keyboard solution

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.