SoFunction
Updated on 2025-04-10

Android programming: ListView and EditText posts to hide soft keyboard functions

This article describes the function of ListView and EditText publishing posts hidden soft keyboards. Share it for your reference, as follows:

In Android development, manually calling the hiding and display of the software disk is sometimes a very common requirement.

The EditText control implements the function of clicking to open the soft keyboard input, but why? Why can EditText click to pop up the keyboard, but TextView cannot? What modifications have EditText made to inherit TextView? For these questions, you need to check the relevant specific code to implement them. The seemingly simple controls are actually very complicated to implement. Here I would like to remind developers who are learning android all the way to be good at thinking about the essence behind the incident, and encourage them together.

There is a layout for the development that the outermost layer is FrameLayout, which wraps the ListView, and the bottom of the bottom is an edit input box. When the input box opens the keyboard, you need to slide down the listView to hide the keyboard, but slide up and continue to slide the listView.

At the beginning, I tried setting a clickListener for listview, setting an onScrollListener, and rewriting its onTouchEvent method, but found that it could not achieve the effect I was satisfied with (I found that the code had been a while ago, and I had a feeling of being confused about modifying the code and not giving up until I was satisfied).

Why not satisfied? Because calling hide keyboard every time the listview slides will cause the listview to flash ghosting, because the listview is sliding and calling hide keyboard, and the Activity sets adjustResize to re-onLayout the entire layout. (PS: I have also been puzzled about this problem before. The adjustResize property causes the bottom Edit to not move with the keyboard, but waits for the keyboard to open and Edit to the appropriate position. The final problem is that it needs to be adjusted and the adjustPan property is used. I believe that similar WeChat chat interfaces will definitely do the same. If there is a better way, please leave a message to discuss!)

Re-intercept touch method of the outermost FrameLayout onInterceptTouchEvent intercept touch method. Post the code directly

@Override
  public boolean onInterceptTouchEvent(MotionEvent ev) {
  //Keyboard open && First scroll dy》0  if(showSoftInput) {
    if(mVelocityTracker == null) {
      mVelocityTracker = ();
    }
    (ev);
    switch (()) {
    case MotionEvent.ACTION_DOWN:
      downY = (int) ();
      showDispatchTouch = true;
      break;
    case MotionEvent.ACTION_MOVE:
      if(downY > ()) {
        showDispatchTouch = true;
      } else {
        showDispatchTouch = false;
        (1000);
        if(() > 200 && mContext instanceof ActivityReplyDetail) {
          ((ActivityReplyDetail)mContext).hideSoftInput();
        }
      }
      break;
    default:
      break;
    }
  }
    return !showDispatchTouch || isAnimating || (ev);
  }

Of course, the touch method can only be intercepted when the keyboard pops up, so we need to check whether the keyboard has popped up. How to detect

(new OnLayoutChangeListener() {
      @Override
      public void onLayoutChange(View v, int left, int top, int right,
          int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
        if(!mOrientationChanged) {
          if(top > oldTop) {
            if(!mEditTouchedFlag)
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
            //Hide the keyboard            mSoftInputShow = false;
            (true);
            (false);
          } else if (top < oldTop) {
            //Keyboard display            mSoftInputShow = true;
            (true);
          }
          mEditTouchedFlag = false;
        }
      }
    });

OnLayoutChangeListener listens for changes in the layout of the control. There is also a monitor for the ViewgetViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener()The timing of this listening method is called after I tested it.

Here, you can also switch horizontally and vertically. When you click the input box, you have to return to the vertically and then pop up the keyboard. The above two listeners are used, and there is also an onConfigurationChanged.

so when you finally achieve your satisfaction according to your own debugging step by step.

EditTextCollapse keyboard is actually simpler, just rewrite onTouchEvent

@Override
public boolean onTouchEvent(MotionEvent event) {
    if(mSoftInputShow) {
      switch (()) {
      case MotionEvent.ACTION_CANCEL:
      case MotionEvent.ACTION_UP:
        (getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        return true;
      }
    }
    return (event);
}
InputMethodManager.HIDE_NOT_ALWAYS

This parameter tells us that hideSoft will not be called all the time, hahaha.

In fact, Edit is a very high Edit. This method will cause the same as the listview. Edit will fold the keyboard while sliding, causing ghosting, because it is the reason for adjustResize and layout.

So in fact, if the system can give us a good method to encapsulate here, click show keyboard, click hide keyboard and will not slide Edit. So if you want to modify the code, you must understand why edit can realize the function of clicking show keyboard.

For more information about Android related content, please check out the topic of this site:Android control usage summary》、《Android development introduction and advanced tutorial》、《Android View View Tips Summary》、《Android programming activity operation skills summary》、《Android database operation skills summary"and"Android resource operation skills summary

I hope this article will be helpful to everyone's Android programming design.