你能拦截菜单上的长按吗?

时间:2011-03-07 16:28:11

标签: android

我正在使用应该是非常简单的代码,但它只是不想工作。操作系统阻止拦截吗?

@Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {


    if (keyCode == KeyEvent.KEYCODE_MENU) 
    {
    longOptionPress = true;
    openOptionsMenu();
        return true;
    }
    return super.onKeyLongPress(keyCode, event);
}

2 个答案:

答案 0 :(得分:10)

您必须使用正常的startTracking()方法致电onKeyPress()事件:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_MENU) {
        event.startTracking();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

另见a question I asked and answered some time ago

答案 1 :(得分:1)

如果是ListView,则需要使用onCreateContextMenu

 public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo)