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
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:id= "@+id/traceroute_ Rootview " android:layout_width=" fill_parent " android:layout_height=" Fill_parent " android: background= "@color/white" android:clickable= "true" android:gravity= "Center_horizontal" android:o rientation= "Vertical" >
Plus ID and clickable=true
Then, in OnCreate, add the listener for the onclick event:
@Override public void OnClick (View v) { switch (V.getid ()) { R.id.traceroute_rootview: Inputmethodmanager IMM = (inputmethodmanager) getsystemservice (context.input_method_service); Imm.hidesoftinputfromwindow (V.getwindowtoken (), 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 Boolean dispatchtouchevent (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 components will not have touchevent if (GetWindow (). superdispatchtouchevent (EV)) { return true; } return ontouchevent (EV); }
Isshoudhideinput (View v,motionevent e) Method:
Public Boolean isshouldhideinput (View V, motionevent event) { if (v = null && (v instanceof EditText)) { C2/>int[] Lefttop = {0, 0}; Gets the current location position of the input box V.getlocationinwindow (lefttop); int left = lefttop[0]; int top = lefttop[1]; int bottom = top + v.getheight (); int right = left + v.getwidth (); if (Event.getx () > Left && event.getx () < right && event.gety () > Top && event.gety ( < bottom) { //Click on the Input box area, leave the event of click EditText 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