单击按钮时关闭弹出窗口问题

时间:2011-03-09 23:05:09

标签: android

我创建了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);


             }
         });

2 个答案:

答案 0 :(得分:1)

尝试使用解雇:

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

你需要在最终的pw中添加一个final修饰符,这样你就可以在onclick方法中使用它了:

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

答案 1 :(得分: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);