Android to judge the soft keyboard pop-up and hide the simple perfect solution

Source: Internet
Author: User

There is an edit box in the recent project, and the following is a ListView. After triggering the edit box to eject the soft keyboard, the ListView can also slide, and the ListView item can also respond to a click. This kind of experience is not effective. Then you want to see if the keyboard pops up when you swipe or click Item, and then hide it if it pops up.

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

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

method does not work, so start with the official documentation.

Discover Inputmethodmanager has a method isactive (view view): Returns True if view is an 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))    Hide Keyboard

However, after the first pop-up keyboard and other touch operations, the keyboard did disappear. But at this point another touch operation (i.e. click on item), the input of view is still active. Because the Click event of item in my Code is

public void Onitemclick (...) {  if (isActive (EditText)) {    hide keyboard  }else{    other Actions  }}

So my item's other operations were not executed. Then look at the Inputmethodmanager function and find a function restartinput (view view): If the current input method is connected to the view, restart the IME with the new content. Restart? Then the input method of view will become the default inactive? So I added this function behind the hidden input method, and sure enough, it succeeded.

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

Attached code:

Inputmethodmanager Inputmethodmanager = (Inputmethodmanager) getactivity (). Getsystemservice (Context.INPUT_METHOD_ SERVICE);
Private Boolean Hidekeyboard () {if (inputmethodmanager.isactive (Searchedittext)) {

Android to judge the soft keyboard pop-up and hide the simple perfect solution

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.