要为Android PopupMenu添加消失监听事件,可以使用PopupMenu的setOnDismissListener方法。具体步骤如下:
PopupMenu popupMenu = new PopupMenu(context, anchorView);
popupMenu.getMenuInflater().inflate(R.menu.popup_menu, popupMenu.getMenu());
popupMenu.setOnDismissListener(new PopupMenu.OnDismissListener() {
@Override
public void onDismiss(PopupMenu menu) {
// 在这里处理Popup菜单消失后的逻辑
}
});
popupMenu.show();
这样当Popup菜单消失时,onDismiss方法里的逻辑将会被执行。