开发者

Can you intercept the long press on menu?

开发者 https://www.devze.com 2023-02-15 13:15 出处:网络
I\'m using what should be pretty simple code, but it just doesn\'t want to work. Does the OS block intercepting this?

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)
0

精彩评论

暂无评论...
验证码 换一张
取 消