您好,登錄后才能下訂單哦!
網上大多數都是說如何監聽點擊外部消失,但我遇到的問題是點擊外面不讓其消失,如下是我的解決方案,關鍵是: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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。