2019-07-06安卓popupwindow与软键盘整合

最近做了一个小作业的消息模块,发现输入消息的popupwindow总是和 软键盘冲突,找了好久才找到解决方案,这里做一个记录。
效果预览


image.png

image.png

以下是代码部分,这里我尽量精炼核心部分

//popupwindow创建
 View popView = View.inflate(context, R.layout.popup_leave_msg, null);
            editText = popView.findViewById(R.id.et_leave_msg);
            popupLeaveMsgWindow = new PopupWindow(popView, ViewGroup.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
            popupLeaveMsgWindow.setTouchable(true);
            popupLeaveMsgWindow.setOutsideTouchable(true);
            popupLeaveMsgWindow.setFocusable(true);//一定要设置焦点
            popupLeaveMsgWindow.setBackgroundDrawable(new BitmapDrawable(context.getResources(), (Bitmap) null));
            popupLeaveMsgWindow.setAnimationStyle(R.style.anim_popup_window);
//popupwindow展示
Activity activity = (Activity) context;
            popupLeaveMsgWindow.showAtLocation(activity.findViewById(R.id.buttom_navigation_bar), Gravity.BOTTOM, 0, 0);
            showBackgroundAnimator();
            mIsShowing = true;
//popup解决软键盘冲突
 popupLeaveMsgWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED); //解决不压键盘
        popupLeaveMsgWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);  //解决不压键盘
        InputMethodManager inputMethodManager = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED,1000);
//设置事件,键盘消失。
popupLeaveMsgWindow.setOnDismissListener(() -> {
            setWindowBackgroundAlpha(1.0f);
            inputMethodManager.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY,0);
        });

照上文四个步骤一步步来就好了。

你可能感兴趣的:(2019-07-06安卓popupwindow与软键盘整合)