Android to judge the state of a soft keyboard and a simple example of hiding a soft keyboard _android

Source: Internet
Author: User

Before I also encountered a problem about getting a soft keyboard state, on the internet to find a lot of information, basically the answer is to use GetWindow (). GetAttributes (). softinputmode== WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED to determine if the soft keyboard is open, if the equality is open, then you can follow this code for subsequent operations. But I tried for a long time, whether the soft keyboard pops up or closes the GetWindow (). GetAttributes (). The value of Softinputmode is always 0, as for why the value has been 0 I don't know,

But then I found a new way to fix it:

Create a Inputmethodmanager object directly, Inputmethodmanager IMM = (Inputmethodmanager) context.getsystemservice (context. Input_method_service), by means of the IsActive () method of the object, Imm. IsActive () If put back to true, the state of the soft keyboard is open, then it is good to do, directly according to IMM. IsActive () Return status for subsequent operations (close the soft keyboard),

The code is as follows:

public void Hideinputwindow {
    if (context==null) {return
     ;
    } 
    Final View v = ((activity) context). GetWindow (). Peekdecorview ();
    if (v!= null && v.getwindowtoken ()!= null) {
      Inputmethodmanager IMM = (inputmethodmanager) context.getsystem Service (context. Input_method_service);
      Imm.hidesoftinputfromwindow (V.getwindowtoken (), 0);
    }
  }

The above is small series for everyone to bring the Android to judge the status of soft keyboard and hide the simple example of a soft keyboard all content, I hope that we support the cloud Habitat Community ~

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.