我通過攔截菜單鍵並在活動上調用openOptionsMenu()來解決此問題。這裏的關鍵是聽衆:
OnKeyListener mMenuKeyListener = new OnKeyListener() {
@Override
public boolean onKey(View view, int keyCode, KeyEvent event) {
if(keyCode==KeyEvent.KEYCODE_MENU) {
activity.openOptionsMenu();
return true;
} else {
return false;
}
}
};
我覺得你有這個按鍵偵聽器添加到每個視圖中PopupWindow得到它的工作,所以我寫了一個函數來做到這一點:
public void setupMenuKeyListenerRecursive(View v) {
if (v != null) {
try {
ViewGroup viewGroup = (ViewGroup)v;
int childCount = viewGroup.getChildCount();
for (int index = 0; index < childCount; index++) {
View child = viewGroup.getChildAt(index);
setupMenuKeyListenerRecursive(child);
}
} catch (Exception e) {
}
v.setOnKeyListener(mMenuKeyListener);
}
}
來源
2012-06-13 18:30:41
Ken