Recently there is a need to pop the soft keyboard when you click EditText, and then hide the soft keyboard when you click on an empty space or another control. This requirement is very useful on tablets, because the screen is large and users cannot hide at the bottom left corner every time, and it's easier to click in the blanks.
Just beginning to search from the Internet, not very ideal, then suddenly think of the Android event distribution mechanism, and then think of the implementation method:
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 trajectory or 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 && ( 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 EditText event, 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_method_ser VICE); Im.hidesoftinputfromwindow (token, inputmethodmanager.hide_not_always); } } ...... }
The following explanation of the code, the first method to achieve the activity of the Dispatchtouchevent method, in fact, not necessarily acitivty,3.0 fragment can also, the main purpose is to intercept user touch events. Specific Android event distribution mechanism see the blog: http://www.cnblogs.com/coding-way/archive/2012/07/04/2575769.html
Now, when the user touches, dispatchtouchevent will be called, then the method has a more detailed comment, no longer repeat.
Original link: http://www.cnblogs.com/coding-way/archive/2012/07/10/2585511.html
Go Android Perfect Hidden Soft keyboard method Click the edit box outside the soft keyboard disappears