Android Click on the blank space to hide the keyboard

Source: Internet
Author: User
Tags gety

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

Related Article

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.