Android Click on the blank space, hide the soft keyboard

Source: Internet
Author: User

When you log in or register, a soft keyboard can often block some of the interface. We need to hide the soft keyboard after the input is complete.

Hide the soft keyboard when we click on an empty space or a non-edittext place.

public class Homeactivity extends Activity {... @Override public boolean dispatchtouchevent (motionevent EV) {if (ev.getaction () = = Motionevent.action_down) {//Get the currently focused view, typically edittext (special case is the trajectory                Or the entity case will move the focus) View V = getcurrentfocus ();              if (Isshouldhideinput (v, Ev)) {hidesoftinput (V.getwindowtoken ());      }} return super.dispatchtouchevent (EV);      /** * To determine if the keyboard is hidden, based on the coordinates of the edittext and the user-clicked coordinates, because there is no need to hide the edittext when the user clicks the * * @param v * @param event * @return */Private Boolean isshouldhideinput (View V, motionevent event) {if (v! = NULL &&am P (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) {                  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 been drawn, the first focus is not on the EditView, and the user uses the trackball to select the other focus return false;          }/** * One of several hidden software disk methods * * @param token */private void Hidesoftinput (IBinder token) { if (token! = null) {Inputmethodmanager im = (inputmethodmanager) getsystemservice (Context.input_metho              D_service);          Im.hidesoftinputfromwindow (token, inputmethodmanager.hide_not_always);   }      }    ......  }

  

Instead of using the EVENT.GETX () function in Isshouldhideinput, you should use the EVENT.GETRAWX () function

Public final float getrawx ()   Added in API Level 1  Returns the original raw X coordinate of this event. For touch events on the screens, this is the original location of the event on the screens, before it had been adjusted for The containing window and views.  

  

This is a point relative to the upper-left corner of the screen public final float GetX ()   Added in API Level 1  GetX (int.) for the first pointer index (could be a Arbi Trary pointer identifier).  This is the coordinate of the touch point relative to the widget Inputmethodmanager im = (inputmethodmanager) getsystemservice (context.input_method_service);          Im.hidesoftinputfromwindow (Getcurrentfocus (). Getwindowtoken (),  inputmethodmanager.hide_not_always);  

  

Android Click on the blank space, hide the soft keyboard

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.