下面是我的代码, 菜单 - ImageView 菜单底部 - 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;
}
});
}
}
问题: 单击菜单按钮时,将显示弹出窗口并显示 menubottom。 再次按下菜单时,弹出菜单会隐藏然后再次显示。
在弹出菜单外按下时,它按预期工作(隐藏弹出菜单和菜单底部)
我认为,setOnDismissListener 会再次触发 menu.setOnClickListener。 提前致谢。
最佳答案
我认为问题出在弹出之后,因为菜单按钮在 popupwindow
之外,popupwindow
被关闭并且菜单按钮的 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;
}
});
}
关于android - popupWindow.setOnDismissListener 再次触发popupmenu,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30128020/