EditText in Fragment cannot pop up soft keyboard when clicked

Let me first talk about how this problem arises. Fragment is nested in Fragment, and the innermost layout is as shown in the figure below.
insert image description here
Open normally for the first time, after sliding the list. It is found that Edittext cannot pop up the soft keyboard.
Since it is a Fragment in Fragment, various soft keyboard callbacks are not easy to use.

Let me talk about the solution first:

Rewrite the edittext control and rewrite its touch event

public class LXTouchEditText extends android.support.v7.widget.AppCompatEditText {
    
    
    public LXTouchEditText(Context context) {
    
    
        super(context);
    }

    public LXTouchEditText(Context context, AttributeSet attrs) {
    
    
        super(context, attrs);
    }

    public LXTouchEditText(Context context, AttributeSet attrs, int defStyleAttr) {
    
    
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
    
    
        if(MotionEvent.ACTION_DOWN == event.getAction()) {
    
    
            clearFocus();  //在滑动设备列表的时候,editview无法弹出软键盘
        }
        return super.onTouchEvent(event);
    }

}

Note that it is necessary to judge that the event is a press action, otherwise, the clearFocus() will be called multiple times because the edittext is very sensitive. Then it clears the focus once for each touch event. In this way, no matter how you slide the listview, the edittext can pop up the soft keyboard normally.

Guess you like

Origin blog.csdn.net/mingtiannihao0522/article/details/115321981