popupWindow設置

一、如果是點擊按鈕等加載完全view之後再彈出彈窗則可以直接

new GuidePopupWindow(MainActivity.this).showAtLocation(MainActivity.this.getWindow().getDecorView(), Gravity.CENTER, 0, 0);

二、如果打開界面就展示彈窗
1、在Activity中:
在onWindowFocusChanged方法中添加:

private GuidePopupWindow mGuidePopupWindow;
private boolean isShow;

@Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (!isShow) {
            mGuidePopupWindow = new GuidePopupWindow(MainActivity.this);
            if ( mGuidePopupWindow != null && MainActivity.this.getWindow().getDecorView() != null && MainActivity.this.getWindow().getDecorView().isAttachedToWindow()) {
                mGuidePopupWindow.showAtLocation(MainActivity.this.getWindow().getDecorView(), Gravity.CENTER, 0, 0);
            }
        }
        isShow = true;
    }

2、在Fragment中:
在Fragment中沒有onWindowFocusChanged方法,但是可以在onCreateView設置一下監聽:

private GuidePopupWindow mGuidePopupWindow;
private boolean isShow;

view.getViewTreeObserver().addOnWindowFocusChangeListener(new ViewTreeObserver.OnWindowFocusChangeListener() {
            @Override
            public void onWindowFocusChanged(boolean hasFocus) {
                if (!isShow) {
                    mGuidePopupWindow = new GuidePopupWindow(getContext());
                    if (getActivity() != null && mGuidePopupWindow != null && getActivity().getWindow().getDecorView() != null && getActivity().getWindow().getDecorView().isAttachedToWindow()) {
                        mGuidePopupWindow.showAtLocation(getActivity().getWindow().getDecorView(), Gravity.CENTER, 0, 0);
                    }
                }
                isShow = true;
            }
        });

3、不使用onWindowFocusChanged方法:
爲避免產生錯誤,需添加Handler()方法,延期執行showAtLocation方法
全部代碼如下(Fragment和Activity都適用):

 private LanguageItemPopupWindow languageItemPopupWindow;
 private Handler languageHandler;
    
private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            if (getActivity() != null && getActivity().getWindow().getDecorView() != null
                    && getActivity().getWindow().getDecorView().isAttachedToWindow() && languageItemPopupWindow != null) {
                languageItemPopupWindow.showAtLocation(getActivity().getWindow().getDecorView(), Gravity.CENTER, 0, 0);
            }
        }
    };
 
    /**
     * 設置多語言選擇
     */

    private void setLanguage() {
        languageHandler = new Handler();
        languageItemPopupWindow = new LanguageItemPopupWindow(getContext(), language, new LanguageItemPopupWindow.CallbackCheck() {
            @Override
            public void dialogCheck(String checkedName) {
                languageHandler.removeCallbacks(runnable);
                 languageItemPopupWindow.dismiss();
            }
        });

        languageHandler.postDelayed(runnable, 100);

        languageItemPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                AppUtil.backgroundAlpha(mActivity, 1f);
                if (languageItemPopupWindow != null) {
                    languageItemPopupWindow.dismiss();
                }
                languageHandler.removeCallbacks(runnable);
            }
        });

    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (languageHandler != null) {
            languageHandler.removeCallbacks(runnable);
        }
        if (languageItemPopupWindow != null) {
            languageItemPopupWindow = null;
        }
 
    }

在myPopupWindow方法裏設置基礎屬性即可例如:

 public myPopupwindowTwo(Context context) {
        super(context);
        mContext = context;
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mView = inflater.inflate(R.layout.dialog_popupwindow, null);
        windowLayout = mView.findViewById(R.id.ll_layout);
        dialog_ok = mView.findViewById(R.id.dialog_ok);

        this.setContentView(mView);
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);//設置寬度
        this.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);//設置高度
        this.setFocusable(true);
        ColorDrawable dw = new ColorDrawable(0x00000000);//一定要設置,不然不會展示半透明
        this.setBackgroundDrawable(dw);
        this.setTouchable(true);
        mView.setFocusableInTouchMode(true);
        this.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
        backgroundAlpha(0.75f);// 0.0-1.0
        this.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

        dialog_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        // PopupWindow消失時,自動監聽
         this.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                backgroundAlpha(1f);
                dismiss();
            }
        });

    }
    /**
     * 設置添加屏幕的背景透明度
     *
     * @param bgAlpha
     */
    public void backgroundAlpha(float bgAlpha) {
        if(mContext != null){
            WindowManager.LayoutParams lp = ((Activity) mContext).getWindow().getAttributes();
            lp.alpha = bgAlpha;
            ((Activity) mContext).getWindow().setAttributes(lp);
        }
    }

如果要設置禁用返回鍵可以修改基礎配置,並添加返回鍵監聽控制方法

/**
     * 基礎信息
     *
     * @param mView
     */
    private void setViewUi(View mView) {
        this.setContentView(mView);
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        this.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        this.setFocusable(false);
        ColorDrawable dw = new ColorDrawable(0x00000000);
        this.setBackgroundDrawable(dw);
        this.setTouchable(true);
        this.setOutsideTouchable(false);
        mView.setFocusable(false);
        mView.setFocusableInTouchMode(true);
        this.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
        this.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        backgroundAlpha(0.5f);
        //  6.0之前,返回鍵的控制
        mView.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    return true;
                }
                return false;
            }
        });
        //  在Android 6.0以上 ,只能通過攔截事件來解決
        this.setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                final int x = (int) event.getX();
                final int y = (int) event.getY();

                if ((event.getAction() == MotionEvent.ACTION_DOWN)
                        && ((x < 0) || (x >= mView.getWidth()) || (y < 0) || (y >= mView.getHeight()))) {
                    return false;
                } else if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
                    return true;
                }
                return false;
            }

        });
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章