您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
网上大多数都是说如何监听点击外部消失,但我遇到的问题是点击外面不让其消失,如下是我的解决方案,关键是:popupMenu.setBackgroundDrawable(null);popupMenu.setFocusable(false);
popupMenu = new PopupWindow(view, keylinearlayout.getWidth(), LayoutParams.WRAP_CONTENT); ColorDrawable cd = new ColorDrawable(R.color.white); //popupMenu.setBackgroundDrawable(cd); popupMenu.setBackgroundDrawable(null); popupMenu.setAnimationStyle(R.style.AnimBottom); popupMenu.update(); popupMenu.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED); popupMenu.setTouchable(true); // 设置popupwindow可点击 popupMenu.setOutsideTouchable(false); // 设置popupwindow外部可点击 popupMenu.setFocusable(false); // 获取焦点 popupMenu.setTouchInterceptor(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { // 如果点击了popupwindow的外部,popupwindow也会消失 // 这里如果返回true的话,touch事件将被拦截 // 拦截后 PopupWindow的onTouchEvent不被调用,这样点击外部区域无法dismiss if (event.getAction() == MotionEvent.ACTION_OUTSIDE) { //popupMenu.dismiss(); StationEventCaptain.getInstance().fireEventDataChange( new StationEventData(IStationEventName.HEDGE_KEYBORAD_DIMISS, true)); return true; } return false; } }); //初始化键盘上的数字 initALLDig(view); } @Override public PopupWindow createPupupWindow() { return popupMenu; } @Override public void showPopupMenu() { if (popupMenu != null && popupMenu.isShowing()) { popupMenu.dismiss(); StationEventCaptain.getInstance().fireEventDataChange( new StationEventData(IStationEventName.HEDGE_KEYBORAD_DIMISS, true)); } else { //popupMenu.showAsDropDown(sourceEditView); int pianyi = dpToPx(activity, 8); if (keyboardType == IPopupMenu.MENU_TYPE_HEDGE_Left) { popupMenu.showAtLocation(keylinearlayout, Gravity.BOTTOM | Gravity.LEFT, pianyi, 0); //设置layout在PopupWindow中显示的位置 } else if (keyboardType == IPopupMenu.MENU_TYPE_HEDGE_RIGHT) { popupMenu.showAtLocation(keylinearlayout, Gravity.BOTTOM | Gravity.RIGHT, pianyi, 0); //设置layout在PopupWindow中显示的位置 } else { popupMenu.showAtLocation(keylinearlayout, Gravity.BOTTOM | Gravity.LEFT, 0, 0); //设置layout在PopupWindow中显示的位置 } // popupMenu.showAsDropDown(sourceView, // Math.round(sourceView.getX()), // (int) (sourceView.getY())); } } /** * 将dp转成像素的 * * @param context * @param dp * @return */ static int dpToPx(final Context context, final float dp) { return (int) (dp * context.getResources().getDisplayMetrics().density); } @Override public void destroyPopupMenu() { popupMenu.dismiss(); StationEventCaptain.getInstance() .fireEventDataChange(new StationEventData(IStationEventName.HEDGE_KEYBORAD_DIMISS, true)); }
成功解决,有问题可以讨论。
以上这篇android popuwindow点击外部窗口不消失的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持亿速云。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。