Welcome to blogging with the Markdown Editor
Many times, when we use the application, there will be an input method soft keyboard pop-up problem, usually, we will let the user click the Return button or the next step to hide the soft keyboard. For a better experience, we can implement when the user finishes using the soft keyboard. Click on an empty area to achieve hidden functionality. Effect:
Code implementation
code block syntax follows the standard markdown code, for example:
PackageExample.com.jinlin.myapplication;ImportAndroid.content.Context;ImportAndroid.os.Bundle;ImportAndroid.os.IBinder;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.view.MotionEvent;ImportAndroid.view.View;ImportAndroid.view.inputmethod.InputMethodManager;ImportAndroid.widget.EditText;/** * Created by J!nl!n on 15/5/21. * * Public Abstract class baseactivity extends appcompatactivity { @Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Iniview (); } Public Abstract void Iniview();@Override Public Boolean dispatchtouchevent(Motionevent ev) {if(ev.getaction () = = Motionevent.action_down) {View v = getcurrentfocus ();if(Isshouldhidekeyboard (v, Ev)) {Hidekeyboard (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 the user cannot hide the edittext when they click the * * @param v * @param event * @return * / Private Boolean Isshouldhidekeyboard(View V, motionevent event) {if(V! =NULL&& (vinstanceofEditText)) {int[] L = {0,0}; V.getlocationinwindow (l);intleft = 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 to ignore it. return false; }Else{return true; } }//If the focus is not edittext then ignore, this occurs when the view has just been drawn, the first focus is not on the EditText, and the user uses the trackball to select other focus return false; }/** * Get Inputmethodmanager, hide soft keyboard * @param token */ Private void Hidekeyboard(IBinder token) {if(Token! =NULL{Inputmethodmanager im = (inputmethodmanager) getsystemservice (Context.input_method_service); Im.hidesoftinputfromwindow (token, inputmethodmanager.hide_not_always); } }}
Of course we have a simpler way to implement this function, just rewrite the Ontouchevent method. The code is as follows:
//click on an empty area to automatically hide the soft keyboard public boolean ontouchevent (Motionevent event) {if (null ! = this . Getcurrentfocus ()) {/** * Click on an empty location to hide the soft keyboard */ Inputmethodmanager Minputmethodmanager = (inputmethodmanager) getsystemservice (Input_metho D_service); return Minputmethodmanager.hidesoftinputfromwindow ( this . Getcurrentfocus (). Getwindowtoken (), 0 ); } return super . Ontouchevent (event); }
Use a baseactivity to do some common operations, other activity is inherited from the base class activity, then all the interface can be realized by clicking on the blank area, hide the soft keyboard.
Sample code Download
Android Click on the blank area to hide the input method soft keyboard