6

我的应用程序中有这个PopupMenu,想知道它何时关闭。使用 API14+,当使用setOnDismissListener(). 但是,我需要知道 PopupMenu 何时从 API11+ 关闭,因此我无法使用侦听器,需要替代侦听器。

我已经尝试过:

  • 覆盖dismiss()PopupMenu的方法,但关闭时不调用。
  • 使用PopupMenu.OnMenuItemClickListener,但当用户在菜单外单击(以关闭它)或单击“返回”时,它不会被激活。

我没有任何其他想法来检测菜单已关闭。所以我希望别人有一个聪明的把戏。否则我无法使用 PopupMenu ...

4

1 回答 1

1

You can try to override method

public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing)

of PopupMenu class. Exactly this method is calling setOnDismissListener() internally in API 14+. But this is not easy to do. Problem is what class com.android.internal.view.menu.MenuBuilder is not included to Android SDK, so you will not be able to compile your code if you will trying to import this class.

However, you can create your own "fake" implementation of this class, place it in package com.android.internal.view.menu, make a .jar file with it, copy this "fake library" to libs folder and set scope of this library in Project Settings of IntelliJ IDEA as "provided". This means that classes in this jar file already have real devices (what is true) and you only need them to compile your code. Main idea here - your fake MenuBuilder should be in classpath for javac compilation, but should not be included in resulting .apk file.

This is not easy solution, but it should work.

于 2012-10-26T15:15:03.817 回答