SoFunction
Updated on 2025-04-07

Example of Android soft keyboard status popping and disappearing

I have encountered a problem with soft keyboard recently. I need to get the status of the soft keyboard. Whether it is being displayed or not, and record it for easy reference in the future. Common methods for determining status on the Internet

Copy the codeThe code is as follows:

getWindow().getAttributes().softInputMode== .SOFT_INPUT_STATE_UNSPECIFIED 

To determine whether the soft keyboard is open, if it is equal, it is open. After trying it, I found that this only works on the keyboard that comes with the phone and has no effect on the input method of the installed third-party.

There is also an introduction to using InputMethodManager to get the keyboard status. The code is as follows

 InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
 boolean isOpen=();//isOpenIf you returntrue,This means that the input method is open

This kind of state that cannot get the keyboard in real time still has no effect on me.

The solution I found later was to monitor the changes in the screen, and the code was as follows:

import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;

/**
  *
  * Soft keyboard monitoring
  */

public class KeyBoardShowListener {
  private Context ctx;

  public KeyBoardShowListener(Context ctx) {
     = ctx;
  }
  OnKeyboardVisibilityListener keyboardListener;

  public OnKeyboardVisibilityListener getKeyboardListener() {
    return keyboardListener;
  }

  public interface OnKeyboardVisibilityListener {


    void onVisibilityChanged(boolean visible);
  }

  public void setKeyboardListener(final OnKeyboardVisibilityListener listener, Activity activity) {
    final View activityRootView = ((ViewGroup) ()).getChildAt(0);

    ().addOnGlobalLayoutListener(new () {

      private boolean wasOpened;

      private final int DefaultKeyboardDP = 100;

      // From @nathanielwolf answer... Lollipop includes button bar in the root. Add height of button bar (48dp) to maxDiff
      private final int EstimatedKeyboardDP = DefaultKeyboardDP + (.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ? 48 : 0);

      private final Rect r = new Rect();

      @Override
      public void onGlobalLayout() {
        // Convert the dp to pixels.
        int estimatedKeyboardHeight = (int) TypedValue
            .applyDimension(TypedValue.COMPLEX_UNIT_DIP, EstimatedKeyboardDP, ().getDisplayMetrics());

        // Conclude whether the keyboard is shown or not.
        (r);
        int heightDiff = ().getHeight() - ( - );
        boolean isShown = heightDiff >= estimatedKeyboardHeight;

        if (isShown == wasOpened) {
          ("Keyboard state", "Ignoring global layout change...");
          return;
        }

        wasOpened = isShown;
        (isShown);
      }
    });
  }
}

The usage is as follows:

//Superce the status of the soft keyboardnew KeyBoardShowListener().setKeyboardListener(
    new () {
      @Override
      public void onVisibilityChanged(boolean visible) {
        if (visible) {
          //The soft keyboard has popped up          
        } else {
          //The soft keyboard does not pop up          
        }
      }
    }, );

Here are some situations you may encounter:

Bind soft keyboard to EditText

(true);
(true);
();
InputMethodManager inputManager = (InputMethodManager)().getSystemService(Context.INPUT_METHOD_SERVICE);
(edit, 0);

Remove soft keyboard display:

("");
();
//close InputMethodManager
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
((), 0);

The software keyboard never pops up

Copy the codeThe code is as follows:

EditText edit=(EditText)findViewById(); (InputType.TYPE_NULL);

Also:

InputMethodManager imm = (InputMethodManager)().getSystemService(Context.INPUT_METHOD_SERVICE);
if(()){ //This can be judged or not((), 0 );
}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.