关闭弹出窗口,没有单击按钮

时间:2014-03-13 21:20:03

标签: android popupwindow

3 个答案:

答案 0 :(得分:3)

private OnClickListener btn2_Click = new OnClickListener() {

    @Override
    public void onClick(View v) {
         LayoutInflater inflater = (LayoutInflater) MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          View layout = inflater.inflate(R.layout.popup, null);
          window = new PopupWindow(layout, 500, 400, true);

         // window.setTouchable(true);
         // window.setOutsideTouchable(true);
          window.setBackgroundDrawable (new BitmapDrawable());
         // window.setFocusable(false);
          window.setOutsideTouchable(true); 
          window.setTouchInterceptor(new OnTouchListener() {
              public boolean onTouch(View v, MotionEvent event) {
                  if (event.getAction() == MotionEvent.ACTION_DOWN) {
                      window.dismiss(); 
                      return true;
                      }
                  return false; 
                  } 
              }); 
          window.showAtLocation(layout,17, 0, 0);

          Button btn = (Button) layout.findViewById(R.id.buttonclose);
          btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                window.dismiss();
            }

          });

    }


};

答案 1 :(得分:1)

如果您的弹出窗口具有列表视图,则接受的答案实际上会引入一个拦截您的点击侦听器的错误。这意味着单击listview / expandablelistview将忽略该对话框。你实际上并不需要设置触摸拦截器。

所以代码片段看起来像:

window.setBackgroundDrawable(new BitmapDrawable());
window.setOutsideTouchable(true); 

那就是它!

如果你仍然希望拦截触摸(没有理由),那么你应该做这样的事情:

window.setTouchInterceptor(new OnTouchListener() { 
    public boolean onTouch(View view, MotionEvent event) { 
       if (event.getAction() == MotionEvent.ACTION_DOWN) {
          window.dismiss();
          boolean consumedEvent = true;
          if (view.hasOnClickListeners()) {
              consumedEvent = view.onTouchEvent(event);
          } 
        return consumedEvent; 
    } 
    return false;    
  } 
}); 

答案 2 :(得分:0)

window.setTouchable(true);
window.setTouchInterceptor(new OnTouchListener() 
{ 
    public boolean onTouch(View v, MotionEvent event) 
    { 
        if (event.getAction() == MotionEvent.ACTION_DOWN) 
        { 
            window.dismiss(); 
            return true; 
        } 
        return false;    
    } 
}); 

BUNU window.showAtLocation in Altina koy。