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

2019-07-06  本文已影响0人  奥雷里亚诺下划线_上校

最近做了一个小作业的消息模块,发现输入消息的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);
        });

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

上一篇下一篇

猜你喜欢

热点阅读