2011-03-09 142 views
1

我创建了info_popup.xml(简单的说,我在弹出窗口上有textview和imagebutton)。我在我的主要活动中显示,但我不知道如何在点击btnExitInfo按钮时关闭该弹出窗口。点击监听器关闭pw的内容是什么?我尝试了与GONE,但它不起作用,它仍然存在。按钮点击关闭弹出问题

LayoutInflater inflater = (LayoutInflater) currentActivity 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      PopupWindow pw = new PopupWindow(inflater.inflate(R.layout.info_popup, 
        null, false), 230, 230, true); 

      pw.showAtLocation(currentActivity.findViewById(R.id.main), 
        Gravity.CENTER, 0, 0); 
      final View popupView=inflater.inflate(R.layout.info_popup, null, false); 

      ImageButton btnExitInfo=(ImageButton)popupView.findViewById(R.id.btnExitInfo); 
      btnExitInfo.setOnClickListener(new View.OnClickListener() { 
       public void onClick(View v) { 
        //popupView.setVisibility(View.GONE); 


      } 
     }); 

回答

1

尝试使用辞退:

public void onClick(View v) { 
    pw.dismiss(); 
} 

你需要一个final修饰符添加到最终PW,所以你可以把它的onclick方法中:

final PopupWindow pw = new PopupWindow........... 
0

下面的代码为我工作:

在班级声明pw: PopupWindow pw;

   LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

       final View vw = inflater.inflate(R.layout.activity_child, null, false); 

       Button close = (Button) vw.findViewById(R.id.btnClose); 
       close.setOnClickListener(new View.OnClickListener() { 
        public void onClick(View view) { 
         if(pw.isShowing()){ 
          pw.dismiss(); 
         } 
        } 
       }); 

       Button next = (Button) vw.findViewById(R.id.btnProceed); 
       next.setOnClickListener(new View.OnClickListener() { 
        public void onClick(View view) { 
         Intent myIntent = new Intent(view.getContext(), NextActivity.class); 
         startActivity(myIntent); 
        } 
       }); 

       pw = new PopupWindow(vw, LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT, true); 
       pw.setBackgroundDrawable(getResources().getDrawable(R.drawable.gradient_bg)); 
       pw.setOutsideTouchable(false); 
       pw.showAtLocation(sv, Gravity.CENTER, 10, 10);