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