从android中的ContextMenu获取选项卡索引

时间:2013-08-17 09:55:21

标签: android eclipse tabs

我向标签添加了一个上下文菜单,我想在用户点击menuitem“关闭标签”时关闭标签页。但我无法获得用户longpress的选项卡索引。这是我的代码。

    @Override
    public void onCreateContextMenu (ContextMenu menu, View v, ContextMenuInfo menuInfo) {
        menu.setHeaderTitle("Options");
        menu.add("Close Tab");
    }
    @Override
    public boolean onContextItemSelected (MenuItem item) {
        if (item.toString().equals("Close Tab")){
             TabHost tabHost = getTabHost();
             int index = code required ?????????
                     int Itemcount = tabHost.getTabWidget().getTabCount();
             if (Itemcount > 1){;
                  tabHost.setCurrentTab(index);
                  tabHost.getTabWidget().removeView(tabHost.getTabWidget().getChildTabViewAt(index));
             }


        }

1 个答案:

答案 0 :(得分:0)

使用item.getTitle()...试试这个

  @Override
public boolean onContextItemSelected (MenuItem item) {
    if (item.getTitle().toString().equals("Close Tab")){
         TabHost tabHost = getTabHost();
         int index = code required ?????????
                 int Itemcount = tabHost.getTabWidget().getTabCount();
         if (Itemcount > 1){;
              tabHost.setCurrentTab(index);
              tabHost.getTabWidget().removeView(tabHost.getTabWidget().getChildTabViewAt(index));
         }


    }