android单项上下文菜单

时间:2012-12-25 23:35:58

标签: android contextmenu onlongclicklistener

我为布局设置了onLongClickListener。长按一下,我需要一个带有“删除”选项的上下文菜单。管理它的最简单方法是什么?感谢

1 个答案:

答案 0 :(得分:2)

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // ....

    mView = someView;
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {

    menu.setHeaderTitle("Menu Title");
    MenuItem remove = menu.add("Delete");

    remove.setOnMenuItemClickListener(new OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            //doStuff...
        }
    });

    super.onCreateContextMenu(menu, v, menuInfo);
}

并在onLongPress或其他内容中,您可以调用openContextMenu