标签:popupwindow isshowing dismiss
点击一个按钮,弹出一个PopupWindow,想在触摸PopupWindow外区域或是再次点击按钮的时候dismiss此PopupWindow。直接上代码:
//点击事件里的代码
if (mWin == null) { initPopupWindow(); } if (mWin.isShowing()) {//这一句真心没用,isShowing()总是返回false Util.write("win is showing"); mWin.dismiss(); } else { Util.write("win is not showing"); mWin.showAtLocation(resultIv, Gravity.BOTTOM, 0, 0); mWin.update(); }
//点击事件代码结束
private void initPopupWindow() { View v = View.inflate(this, R.layout.menu_layout, null); mWin = new PopupWindow(v, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); // 设置显示关闭动画 mWin.setAnimationStyle(R.style.bottom2up); // 设置背景,触摸框外区域也可以关闭弹出框 mWin.setBackgroundDrawable(new ColorDrawable()); mWin.setOutsideTouchable(true); mWin.setFocusable(true); mWin.setTouchInterceptor(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if(event.getAction()==MotionEvent.ACTION_OUTSIDE){ mWin.dismiss(); return true; } return false; } }); }
以上代码亲测,在魅族4、android 4.4.2、minSdkVersion 17、targetSdkVersion 22,有效。
标签:popupwindow isshowing dismiss
原文地址:http://blog.csdn.net/songtzu/article/details/46312309