2015-07-13 65 views
0

我在我的项目中使用PopupWindow。 当触摸显示器时我无法解雇它。我试过:如何解雇PopupWindow whem点击外部?

@Override 
    public void onResume() { 
     super.onResume(); 
     new Handler().postDelayed(new Runnable() { 
      @Override 
      public void run() { 
       View popup = LayoutInflater.from(getActivity()).inflate(R.layout.popup, null, false); 
       final PopupWindow popupWindow = new PopupWindow(popup, (int) DpiToPixel.calculateDpToPixel(150, getActivity()), 
         (int) DpiToPixel.calculateDpToPixel(150, getActivity()), true); 
       popupWindow.showAtLocation(rootView, Gravity.CENTER, 0, 0); 
       popup.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View view) { 
         popupWindow.dismiss(); 
        } 
       }); 
       popupWindow.setOutsideTouchable(true); 
       popupWindow.setTouchable(true); 
       popupWindow.setBackgroundDrawable(new BitmapDrawable()); 
       popupWindow.setTouchInterceptor(new View.OnTouchListener() { 
        @Override 
        public boolean onTouch(View v, MotionEvent event) { 
         Toast.makeText(getActivity(),"Click!",Toast.LENGTH_LONG).show(); 
         popupWindow.dismiss(); 
         return true; 
        } 
       }); 
      } 
     }, 100L); 
    } 

但它不工作。我试过popupWindow.setFocusable(true);我试过setBackgroundDrawablePopupWindow都不行。我怎样才能解雇这个弹出窗口????????????

+0

我的答案是否适合你? –

回答

0

设置一个BackgroundDrawable,然后将setOutsideTouchable参数设置为true。

setOutsideTouchable:指示弹出窗口是否会在窗口外通知触摸事件 。

popupWindow.setBackgroundDrawable(new ColorDrawable()); 
myPopupWindow.setOutsideTouchable(true); 

或者试试这个:

myPopupWindow.setFocusable(true); 
0

推动这一语句将run()方法的末尾:

popupWindow.showAtLocation(rootView, Gravity.CENTER, 0, 0); 

换句话说,不显示弹出直到之后,您已完成所有呼叫配置。