鍍金池/ 問答/Android/ 請問這樣彈出PopupWindow,怎么可以彈出時讓外部控件不可點擊?網(wǎng)上查了好

請問這樣彈出PopupWindow,怎么可以彈出時讓外部控件不可點擊?網(wǎng)上查了好多方法都不行.

    private void showLoading() {
        //TODO:bug:目前彈出window時可以點擊外部控件
        View typeWindow = LayoutInflater.from(mContext).inflate(R.layout.loading, null);
        rlLoading = typeWindow.findViewById(R.id.rl_loading);
        mPopupWindow = new PopupWindow(typeWindow, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        mPopupWindow.setOutsideTouchable(false);
        mPopupWindow.setFocusable(false);
//        mPopupWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));
        mPopupWindow.showAtLocation(view.findViewById(R.id.rl_login), Gravity.CENTER, 0, 0); //popupwindow彈出的位置
        WindowManager.LayoutParams lpType = mWindow.getAttributes(); // 設置背景顏色變暗
        lpType.alpha = 0.7f;
        mWindow.setAttributes(lpType);
        mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                WindowManager.LayoutParams lp = mWindow.getAttributes();
                lp.alpha = 1f;
                mWindow.setAttributes(lp);
            }
        });
    }
回答
編輯回答
不討囍

大概設置下這個就可以了

        popupWindow.setOutsideTouchable(true);
        popupWindow.setFocusable(true);
        //點擊外部,popWindow消失
        popupWindow.setTouchInterceptor(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction()==MotionEvent.ACTION_OUTSIDE){
                    performClick();
                    popupWindow.dismiss();
                    return true;
                }
                return false;
            }
        });
2018年1月7日 23:05
編輯回答
久舊酒

我這里的做法是,設置Pop全屏
添加半透明背景覆蓋Activity/Fragment視圖
給pop背景添加點擊事件,點擊事件不做任何事情

2017年7月1日 08:38