popupWindow.setOnDismissListener再次触发popupmenu

Vig*_*esh 5 android android-layout android-menu android-popupwindow

下面是我的代码,菜单-ImageView menubottom-ImageView我的要求是在显示弹出窗口时显示menubottom图像。

menu.setOnClickListener(new OnClickListener() 
    {   
        public void onClick(View v) 
        {View layout=(View)getLayoutInflater().inflate(R.layout.navigationbar, null);
        popupWindow = new PopupWindow(getApplicationContext());
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setContentView(layout);
        popupWindow.setHeight(LayoutParams.WRAP_CONTENT);
        popupWindow.setWidth(swidth);
        popupWindow.setFocusable(false);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setAnimationStyle(-1);

            if(x==1)
            {   popupWindow.showAsDropDown(menubottom);
                menubottom.setVisibility(View.VISIBLE);
                x=0;                    
            }
            else
            {   
                popupWindow.dismiss();
                popupWindow=null;           
            }
            popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {

                @Override
                public void onDismiss() {
                    // TODO Auto-generated method stub
                    menubottom.setVisibility(View.INVISIBLE);   
                    x=1;

                }
            });

        }

}
Run Code Online (Sandbox Code Playgroud)

问题:单击菜单按钮时,将显示弹出窗口,并显示菜单底部。再次按菜单,弹出菜单将被隐藏,然后再次显示。

在弹出菜单外按时,其按预期方式工作(弹出和菜单底部被隐藏)

我认为,setOnDismissListener再次触发menu.setOnClickListener。提前致谢。

Far*_*C K 2

我认为问题是在弹出之后,因为菜单按钮在外面popupwindowpopupwindow被关闭并且菜单按钮onclick再次触发。所以也许你可以做这样的事情(最初x=1

menu.setOnClickListener(new OnClickListener() 
{   
    public void onClick(View v) 
    {View layout=(View)getLayoutInflater().inflate(R.layout.navigationbar, null);
    popupWindow = new PopupWindow(getApplicationContext());
    popupWindow.setBackgroundDrawable(new BitmapDrawable());
    popupWindow.setContentView(layout);
    popupWindow.setHeight(LayoutParams.WRAP_CONTENT);
    popupWindow.setWidth(swidth);
    popupWindow.setFocusable(false);
    popupWindow.setOutsideTouchable(true);
    popupWindow.setAnimationStyle(-1);

      /*  if(x==1)
        {   popupWindow.showAsDropDown(menubottom);
            menubottom.setVisibility(View.VISIBLE);
            x=0;                    
        }
        else
        {   
            popupWindow.dismiss();
            popupWindow=null;           
        } */

        if(x==0) {
            x=1;
        } else {
        popupWindow.showAsDropDown(menubottom);
            menubottom.setVisibility(View.VISIBLE);

        }
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {

            @Override
            public void onDismiss() {
              // TODO Auto-generated method stub
                menubottom.setVisibility(View.INVISIBLE);   
                x=0;

            }
        });

    }
Run Code Online (Sandbox Code Playgroud)

}