Android to judge the state of the soft keyboard and hide the soft keyboard

Source: Internet
Author: User

  Before I also encountered a question 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 solution: directly create a Inputmethodmanager object, 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 (turn off the soft keyboard), the code is as follows:

public void Hideinputwindow {
       if (context==null) {
    & nbsp   return;
       }&NBSP
       final View v = ((activity) context). GetWindow (). Peek Decorview ();
       if (v!= null && v.getwindowtoken ()!= null) {
        &NBSP ;  inputmethodmanager IMM = (Inputmethodmanager) context.getsystemservice (context. Input_method_service);
           imm.hidesoftinputfromwindow (V.getwindowtoken (), 0);
       }
   }


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.