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
[Java]View Plaincopy
- <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:orientation="vertical" >
- </LinearLayout>
Plus ID and clickable=true
Then, in OnCreate, add the listener for the onclick event:
[Java]View Plaincopy
- Findviewbyid (R.id.traceroute_rootview). Setonclicklistener (this);
In the onclick:
[Java]View Plaincopy
- @Override
- Public void OnClick (View v) {
- switch (V.getid ()) {
- Case 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.
[Java]View Plaincopy
- @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 none of the components will be touchevent.
- if (GetWindow (). superdispatchtouchevent (EV)) {
- return true;
- }
- return ontouchevent (EV);
- }
Isshoudhideinput (View v,motionevent e) Method:
[Java]View Plaincopy
- Public Boolean isshouldhideinput (View V, motionevent event) {
- if (v = null && (v instanceof EditText)) {
- int[] lefttop = { 0, 0};
- //Get the current location 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 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.
Transferred from: http://blog.csdn.net/mad1989/article/details/25069821
Android Click EditText Anywhere outside of the text box to hide the keyboard solution