2015-07-13 86 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); 

換句話說,不顯示彈出直到之後,您已完成所有呼叫配置。