Android hide, Show soft keyboard method

Source: Internet
Author: User

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

Related Article

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.