Can you intercept the long press on menu?
I'm using what should be pretty simple code, but it just doesn't want to work. Does the OS block intercepting this?
@Override
public bo开发者_StackOverflow社区olean onKeyLongPress(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU)
{
longOptionPress = true;
openOptionsMenu();
return true;
}
return super.onKeyLongPress(keyCode, event);
}
You must call startTracking()
on the event from the normal onKeyPress()
method:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU) {
event.startTracking();
return true;
}
return super.onKeyDown(keyCode, event);
}
See also a question I asked and answered some time ago.
If it is ListView, You need to use onCreateContextMenu
.
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo)
精彩评论