Android to judge the soft keyboard pop-up and hide the simple perfect solution (recommended) _android

Source: Internet
Author: User

There is an edit box in the recent project, and the following is a ListView. After the trigger box pops up, the ListView can also slide and the ListView item will respond to the click. This experience is not very effective. So you want to slide or click the item to determine whether the keyboard pops up, if it pops up, hide it.

Online search, found that Android does not directly provide soft keyboard pop-up and hidden judgments, some solutions such as the height of the parent control or judge

if (GetWindow (). GetAttributes (). Softinputmode==windowmanager.layoutparams.soft_input_state_hidden)
 hides the keyboard;

Method was not used, so the official document began.

Found Inputmethodmanager has a method isactive (view view): Returns True if view is the active view of the input method. That is, returns true if the soft keyboard is ejected by the view trigger. Wow, that's a good question.

if (IsActive (edittext))
hides the keyboard

Then let the other view force the focus, so that Isactivite (Ediitext) is false.

This method is relatively simple, the code is relatively short, also very good understanding, hope to be able to help people in need, nor waste I debug a few hours of kung fu.

Attached code:

Inputmethodmanager Inputmethodmanager = (Inputmethodmanager) getactivity (). Getsystemservice (Context.INPUT_METHOD_ SERVICE); <br>private boolean hidekeyboard () {
    if (inputmethodmanager.isactive (Searchedittext)) {<br >//Because it is under fragment, GetView () is used to get view, or Findviewbyid () to get the parent control
      GetView (). Requestfocus (); The other view gets the focus. This is because it is under the fragment, so we use GetView () to specify any other view
      Inputmethodmanager.hidesoftinputfromwindow ( Getactivity (). Getcurrentfocus (). Getwindowtoken (), inputmethodmanager.hide_not_always);
      return true;
    }
    return false;
  }

PS: If it is a manual eject keyboard, getactivity () Getcurrentfocus () is changed to Searchedittext. and the manual eject keyboard Isactivie () is invalid and can be judged by the tag.

The above is a small series for everyone to bring the Android Judge soft keyboard pop-up and hide the simple perfect solution (recommended) All content, I hope that we support 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.