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