The ultimate way to hide a soft keyboard:
Public class softkeyboardutil {/*** Hide Soft keyboard (only for activity, not for fragment)*/ Public Static voidHidesoftkeyboard (activity activity) {View View=Activity.getcurrentfocus (); if(View! =NULL) {Inputmethodmanager Inputmethodmanager=(Inputmethodmanager) Activity.getsystemservice (Activity.input_method_service); Inputmethodmanager. Hidesoftinputfromwindow (View.getwindowtoken (), inputmethodmanager.hide_not_always); } } /*** Hide Soft keyboard (available for activity,fragment)*/ Public Static voidHidesoftkeyboard (context context, list<view>viewlist) { if(Viewlist = =NULL)return; Inputmethodmanager Inputmethodmanager=(Inputmethodmanager) Context.getsystemservice (Activity.input_method_service); for(View v:viewlist) {Inputmethodmanager. Hidesoftinputfromwindow(V.getwindowtoken (), inputmethodmanager.hide_not_always); } }}
That softkeyboardutil the parameter of the second method List<View> viewList
, the viewlist need to put in the current interface all trigger the soft keyboard popup control. For example, a login interface, there is an account entry box and a password input box, need to hide the keyboard, the two input box objects placed in the Viewlist, as a parameter to the Hidesoftkeyboard method can be.
The following method will pop up the hidden, hidden popup
Public Static void Hidekeyboard () { = (Inputmethodmanager) Getsystemservice (context.input_method_service); Imm. Togglesoftinput (0, inputmethodmanager.hide_not_always);}
See the API in detail:
\android-sdk\sources\android-26\android\view\inputmethod\inputmethodmanager.java
Android manual display and hide soft keyboard
72853760
1, method One (if the input method is displayed on the window, then hide, or vice versa)
Inputmethodmanager IMM = (Inputmethodmanager) Getsystemservice (context.input_method_service); Imm. Togglesoftinput(
2, Method Two (view is to accept the soft keyboard input views, show_forced means force display)
Inputmethodmanager IMM = (Inputmethodmanager) Getsystemservice (context.input_method_service); Imm. Showsoftinput(view,inputmethodmanager.show_forced); Imm. Hidesoftinputfromwindow // Force hidden Keyboard
3, call the hidden system default Input method
((Inputmethodmanager) Getsystemservice (Context.input_method_service)). Hidesoftinputfromwindow (Widgetsearchactivity. This
. Getcurrentfocus (). Getwindowtoken (), inputmethodmanager.hide_not_always); // (Widgetsearchactivity is the current activity)
4, get the input method open state
Inputmethodmanager IMM = (Inputmethodmanager) Getsystemservice (Context. Input_method_serviceboolean isOpen = Imm. isActive (); // IsOpen returns True to indicate that the input method is open
Android hide, Show soft keyboard method