Method One: Use the Android distribution mechanism (the code is a little bit more)
/** * Realization Click on the blank, soft keyboard disappears event * @param ev * @return */@Overridepublic Boolean dispatchtouchevent (Motionevent ev) { if (Ev.getaction () == motionevent.action_down) { // Get the current focus of the View, in general is edittext (special case is the trajectory or the entity case will move focus) view v = getcurrentfocus (); if (IsShouldHideInput (V, ev)) { hidesoftinput ( V.getwindowtoken ()); } } return super.dispatchtouchevent (EV);} /** * to determine if the keyboard is hidden, depending on the coordinates of the edittext and the user-clicked coordinates, because there is no need to hide * * @param when the user clicks EditText v * @param event * @return &NBSP;*/PRIVATE&NBSP;BOOLEAN&NBSP;ISSHOULDHIDEINPUT (view v, motionevent event) { if (v != null && (V instanceof edittext) { int[] l = { 0, 0 }; V.getlocationinwindow (L); int left = l[0], Top = l[1], bottom = top + v.getheight (), right = left + v.getwidth (); if (Event.getx () > left && event.getx () < right && event.gety () > top && event.gety ( ) < bottom) &NBSP;{&NBSP;&Nbsp; // Click on the EditText event and ignore it. return false; } else { return true; } } // If the focus is not edittext, this occurs when the view has just finished drawing, the first focus is not on the EditView, and the user uses the trackball to select other focus return false;} /** * one of the many hidden software disk methods * * @param token */private void Hidesoftinput (Ibinder token) { if (token != null) { InputMethodManager im = (Inputmethodmanager) Getsystemservice (Context.input_method_service); Im.hidesoftinputfromwindow (token, inputmethodmanager.hide_ not_always); }}
2. Method Two, (the code is small, but sometimes it fails)
//Preferences define variable, click Blank Keyboard vanishing Event definition
Private Inputmethodmanager Manager;
and initialize it in OnCreate.
Click the blank keyboard vanishing event to initialize
Manager = (Inputmethodmanager) getsystemservice (Context.input_method_service);
/**
* Click on the blank space, keyboard disappear event
* @paramEvent
* @return
*/
@Override
Public Booleanontouchevent(Motionevent event) {
// TODO auto-generated Method stub
if(event.getaction () = = Motionevent.Action_down) {
if(Getcurrentfocus ()! =NULL
&& Getcurrentfocus (). Getwindowtoken ()! =NULL) {
Manager. Hidesoftinputfromwindow (Getcurrentfocus (). Getwindowtoken (), Inputmethodmanager.hide_not_always);
}
}
return Super. Ontouchevent (Event);
}
Android Click on the blank space to hide the keyboard