0

我有 3 个图像按钮,按下它们每个都会打开一个弹出窗口。问题是当我单击按钮 1 时,会出现一个弹出窗口,如果我不关闭该弹出窗口而是单击按钮 2,则会出现按钮 1 和按钮 2 的弹出窗口。按下新按钮时如何关闭任何打开的弹出窗口?

这是我的代码

final ImageButton rredButton=(ImageButton)findViewById(R.id.RredButton);
    rredButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View arg0) {
            LayoutInflater layoutInflater
            = (LayoutInflater)getBaseContext()      
            .getSystemService(LAYOUT_INFLATER_SERVICE);
            View popupView = layoutInflater.inflate(R.layout.popupright, null);
            final PopupWindow popupWindow = new PopupWindow(               
                    popupView,                
                    LayoutParams.WRAP_CONTENT,                       
                    LayoutParams.WRAP_CONTENT);     
            Button btnNxtScr = (Button)popupView.findViewById(R.id.nextscreen);             
            btnNxtScr.setOnClickListener(new Button.OnClickListener(){     
                @Override     
                public void onClick(View v) {      
                    Intent myintent1 = new Intent(colorActivity.this,colorBlueActivity.class);
                    startActivity(myintent1);
                }
            });
                    popupWindow.showAsDropDown(rredButton, 1, -1);
        }});

这是另一个按钮(使用类似的弹出方法)

final ImageButton ryellowButton=(ImageButton)findViewById(R.id.RyellowButton);
    ryellowButton.setOnClickListener(new View.OnClickListener() {
        @Override     
        public void onClick(View arg0) {          
            createWrongPop(arg0);      
            }});
4

1 回答 1

0

为此,您需要在显示弹出窗口时在类中保存对 PopupWindow 的引用,并在弹出窗口消失时将其清除。然后,如果您单击第二个按钮,您可以检查弹出窗口是否可用,如果是,则在其上调用 .dismiss() 并创建一个新按钮,或修改其内容。

于 2012-10-26T04:37:09.357 回答