Tencent Interactive Live 1.8 solves the problem of soft keyboard covering the input box and monitoring soft keyboard pop-up close

On the demo of Tencent Cloud Interactive Live 1.8, the soft keyboard on the live interface will cover the input box. The original idea was to dynamically set the position of the input box dialog, but after modification on the demo, it can be solved on Xiaomi, Meizu, Huawei and other machines The problem of coverage, but there is still a problem of coverage on Samsung machines. Someone may have solved the coverage problem, but the monitor soft keyboard has failed. It means that the soft keyboard dialog is not closed. Here is my solution, first picture above:

Write picture description here


Question 1: The soft keyboard covers the dialog

Write picture description here

Add before setContentView
// to prevent the soft keyboard from
covering the Dialog's input box getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE |
WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

Can solve the problem of soft keyboard coverage;

Question 2: When the input box pops up, click the blank space to close the soft keyboard and dialog

1. Change the root layout of dialog to match_parent

2. Remove the monitoring of the position of the root layout in the old code, and increase the monitoring event of the root layout onTouch

Write picture description here

Question 3: Close the input box or press the return key and the dialog is not closed

After completing steps 1 and 2, it is found that closing the input box or pressing the return key dialog is not closed, and it will appear at the bottom of the screen. At this time, you need to listen to the events of the root layout GlobalLayout

Found in the activity_live.xml layout, the topmost layout is

    <include
        android:id="@+id/invite_views"
        layout="@layout/invite_views"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent" />

At this time, it is enough to monitor the layout of the event;

Note: OnGlobalLayoutListener responds with the following conditions:
SoftInputMode is ADJUST_RESIZE, otherwise the position change cannot be monitored.

//软键盘的监听
        findViewById(R.id.invite_views).getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                Rect r = new Rect();
                //获取当前界面可视部分
                getWindow().getDecorView().getWindowVisibleDisplayFrame(r);
                //获取屏幕的高度
                int screenHeight =  getWindow().getDecorView().getRootView().getHeight();
                //此处就是用来获取键盘的高度的, 在键盘没有弹出的时候 此高度为0 键盘弹出的时候为一个正数
                int heightDifference = screenHeight - r.bottom;

                if(heightDifference <= 0 && mLastDiff > 0){
                    //软键盘收起状态
                    Log.d("AAAA","软键盘 弹出之后收起了 Rect GlobalLayoutListener heightDifference="+heightDifference+",mLastDiff="+mLastDiff);
                    if(inputMsgDialog!=null && inputMsgDialog.isShowing()){
                        inputMsgDialog.dismiss();
                    }
                }
                mLastDiff = heightDifference;

            }
        });

Write picture description here

At this point, the above problem should be resolved.

Guess you like

Origin blog.csdn.net/android_freshman/article/details/51713183