關於點選顯示PopupWindow再次點選消失的問題
阿新 • • 發佈:2019-01-28
點選一個按鈕,彈出一個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,有效。