Android Force soft Keyboard off

Source: Internet
Author: User

In the Android development process, sometimes we have the need to force off the soft keyboard. For example: Now there is a text edit box (testet) and a button (TESTBTN), we now click on the text edit box tested, this will pop up the soft keyboard, then we click on the button testbtn, the soft keyboard still remains open state ... The problem comes, and the result we want is the soft keyboard disappears. (Testbtn is just a random example of me, you can also make other controls such as drop-down boxes, clickable pictures, custom spaces, etc.)

Here are two ways to solve:

First, this method just turns off the soft keyboard:

In the button testbtn call the following method Hidekeyboard ():

private void Hidekeyboard () {
Inputmethodmanager IMM = (inputmethodmanager) this.getsystemservice (Context.input_method_service);
if (imm.isactive () && this.getcurrentfocus () = null) {
if (This.getcurrentfocus (). Getwindowtoken () = null) {
Imm.hidesoftinputfromwindow (This.getcurrentfocus (). Getwindowtoken (), inputmethodmanager.hide_not_always);
}
}
}

Second, this method will determine the state of the soft keyboard, if the closed state is open, if it is open the state is closed:

In the button testbtn call the following method Hidekeyboard ():

private void Hidekeyboard () {
Inputmethodmanager IMM = (inputmethodmanager) this.getsystemservice (Context.input_method_service);
if (imm.isactive ()) {
if (This.getcurrentfocus (). Getwindowtoken () = null) {
Imm.hidesoftinputfromwindow (This.getcurrentfocus (). Getwindowtoken (), inputmethodmanager.hide_not_always);
}
}
}

 

  

Android Force soft Keyboard off

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.