0

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

    @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));
             }


        }
4

1 回答 1

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));
         }


    }
于 2013-08-17T10:34:51.150 回答