一、如果是点击按钮等加载完全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;
}
});
}