关闭弹出窗口,使用相同的按钮

时间:2014-02-17 11:31:27

标签: android popup

我想通过它打开的相同按钮关闭弹出窗口,当我再次单击该按钮时,应该关闭弹出窗口。并且当我点击后退按钮时也想清除那个窗口。任何人都可以帮我解决这个问题吗? 谢谢。

这是我的代码,

ivmainmenu.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            LayoutInflater layoutInflater 
             = (LayoutInflater)getBaseContext()
              .getSystemService(LAYOUT_INFLATER_SERVICE);  
            View popupView = layoutInflater.inflate(R.layout.popupwindow, null);  
          final PopupWindow popupWindow = new PopupWindow(popupView,LayoutParams.FILL_PARENT,
                  LayoutParams.WRAP_CONTENT);  
                popupWindow.showAsDropDown(ivmainmenu, 150,14);
                popupWindow.setOutsideTouchable(true);

                TextView tvpopupwork = (TextView)popupView.findViewById(R.id.tvpopupwork);
                TextView tvpopupabout = (TextView)popupView.findViewById(R.id.tvpopupabout);
                TextView tvpopupservices = (TextView)popupView.findViewById(R.id.tvpopupservices);
                TextView tvpopupcontact = (TextView)popupView.findViewById(R.id.tvpopupcontact);

                tvpopupwork.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        Intent intent = new Intent(Home.this,Ourwork.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                        startActivity(intent);
                    }
                });

                tvpopupabout.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        Intent intent = new Intent(Home.this,Aboutus.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                        startActivity(intent);  
                    }
                });

                tvpopupservices.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub

                        Intent intent = new Intent(Home.this,Services.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                        startActivity(intent);
                    }
                });

                tvpopupcontact.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub

                        Intent intent = new Intent(Home.this,Contact.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                        startActivity(intent);
                    }
                });
           }
        });

3 个答案:

答案 0 :(得分:1)

您还可以创建2个侦听器来侦听不同的事件。 监听器1 - 这将用于在打开弹出窗口后执行任务 监听器2 - 关闭弹出窗口

您可以从另一个侦听器设置侦听器(在按钮单击时切换侦听器)。

listener2 = new OnClickListener() {

@Override
public void onClick(View v) {
    tv.setText("Listener2");
    //assign button to listener1
    button.setOnClickListener(listener1);
}};

listener1 = new OnClickListener() {

@Override
public void onClick(View v) {
    tv.setText("Listener1");
    //assign button to listener2
    button.setOnClickListener(listener2);
}};

//set this listener as the default(initially) listener onCreate
button.setOnClickListener(listener1);

答案 1 :(得分:0)

我会使用像;

这样的布尔变量
boolean toggle = true;

和onClickListener

PopupWindow popupWindow;
if(toggle){
   // Show popup here
   toggle = false;
}
else{
   if(popupWindow != null && popupWindow.isShowing())
      popupWindow.dismiss();
   toggle = true;
}

答案 2 :(得分:0)

使用布尔标志

   if(flag)
{
hideWindow();
flag=false;
}
else
{
showWindow();
flag=true;
}
按下后退时隐藏

Override onBackPressed()