SoFunction
Updated on 2025-03-11

Two ways to pop up and hide the Android monitor soft keyboard

need:

Now there is a requirement to click on a line of text box to pop up an input box that was hidden before. After the input is completed, press the return key or other things to hide the keyboard and input box, and fill the content of the input box into the text box.

accomplish:

The first reaction to getting this requirement is to write a monitor to monitor the display and hiding of the keyboard to control the display and hiding of the input box and control the content in the text box.
So I did the following:

  1. Specifying android:windowSoftInputMode="adjustResize|stateAlwaysHidden" is to change the layout when the keyboard pops up.
  2. Let Activity implement LayoutchangeListener to monitor layout changes. When the layout changes to 1/3 of the screen, we think it is caused by the keyboard.
@Override 
 public void onLayoutChange(View v, int left, int top, int right, 
     int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) { 

   //Old is the value of the upper left and lower right coordinate point before the change. If there is no old, the value of the upper left and lower right coordinate point after the change is the value of the upper left and lower right coordinate point after the change.
   //Now I think that as long as the control pushes the Activity upward height exceeds 1/3 of the screen height, I think the soft keyboard pops up   if(oldBottom != 0 && bottom != 0 &&(oldBottom - bottom > keyHeight)){ 

     (, "The monitor pops up with a soft keyboard...", Toast.LENGTH_SHORT).show(); 

   }else if(oldBottom != 0 && bottom != 0 &&(bottom - oldBottom > keyHeight)){ 

     (, "Speaking that the software disk is closed...", Toast.LENGTH_SHORT).show(); 

   } 

 }

question:

That's right, this can indeed monitor the pop-up and hiding of the soft keyboard. All of this is because indowSoftInputMode=adjustResize was set before, but this property is invalid when the keyboard pops up and hiding is not triggered.

After using SystemBarTintManager in the project, the Activity becomes full screen mode, so I did the following

//contentlayout is the outermost layoutmChildOfContent = (0);
()
.addOnGlobalLayoutListener(new () {  
   public void onGlobalLayout() {    
       possiblyResizeChildOfContent();  
}});

private void possiblyResizeChildOfContent() {  
int usableHeightNow = computeUsableHeight();  
if (usableHeightNow != usableHeightPrevious) {    
   int usableHeightSansKeyboard = ().getHeight();    
   int heightDifference = usableHeightSansKeyboard - usableHeightNow;    
   if (heightDifference > (usableHeightSansKeyboard / 4)) {      
   // The keyboard pops up   } else {      
   // Close the keyboard   ();      
   (().toString());    
}    
   ();    
    usableHeightPrevious = usableHeightNow;  
}
}
private int computeUsableHeight() {  
 Rect r = new Rect();  
(r);  
return ( - );}

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.