如何在TabHost中获取Tab的内容?

时间:2013-11-17 12:00:49

标签: android android-layout android-activity android-tabhost android-view

我有一个由以下内容定义的活动:

<LinearLayout
        android:id="@+id/TabContainer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="99"
        android:orientation="vertical">
  <TabHost
        android:id="@+android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <LinearLayout
        android:id="@+id/TabLinearLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
      <TabWidget
        android:id="@+android:id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></TabWidget>
      <FrameLayout
        android:id="@+android:id/tabcontent"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="10dp"></FrameLayout>
    </LinearLayout>
  </TabHost>
</LinearLayout>

基本上它是一个带有TextViews作为内容的TabHost。我正在使用createTabContent()动态构建这些TextViews的选项卡和内容。这很好。但在某些情况下(某些事件由带有TCP套接字的AsyncTask处理),我需要更改其中一些TextView的内容。

我确信我的问题是我对Android SDK缺乏经验的结果,但我找不到迭代TabHost并获取每个TextView的内容的方法。

我所取得的“最接近”的方法是:

TabHost th = (TabHost) findViewById(android.R.id.tabhost);
TabWidget tw = th.getTabWidget();    

for (int i = 0; i < tw.getChildCount(); i++) {
  LinearLayout lLayout = (LinearLayout) tw.getChildAt(i);
  TextView tv = ((TextView) lLayout.getChildAt(i));      

  tv.append(linea);
}

但是,这样做是将“linea”附加到选项卡的名称(指示符)。我想将其添加到该标签的内容,但我找不到检索与其关联的TextView的方法。

任何想法都将非常感激!

---------编辑---------

虽然在下面的评论中我发布了我已经有解决方案,但我发现它无效。代码现在看起来像这样:

for (int i = 0; i < tw.getChildCount(); i++) {
  LinearLayout LLayout = (LinearLayout) tw.getChildAt(i);
  TextView currenttab = (TextView) LLayout.getChildAt(0);

  if (tab.equals(currenttab.getText())) {
    th.setCurrentTab(i);
    TextView tabContent = (TextView) th.getTabContentView().getChildAt(0);
    tabContent.append(linea);
    return true;
  }
}

问题是getTabContentView()仅引用当时活动的Tab,所以如果我想在非选择的选项卡的TextView中添加一些文本,我必须先使用.setCurrentTab(i)选择它,意味着我要更改活动标签...

我认为这比我想的要容易得多......对此有何帮助?

谢谢!

---------编辑---------

此时我意识到无法访问非活动选项卡的内容。至少不那样。据说您无法访问非活动选项卡的视图,因为它没有关联的视图,至少不是公共视图。

我试图声明一个Map,其中第一个String是选项卡的名称,第二个是与其内容关联的TextView,并在创建选项卡时分配它们。结果是我只能引用活动选项卡的TextView,其他的引发了NullPointer异常。

所以我以不同的方式关注问题。现在,如果我必须将一些文本添加到非活动选项卡中,我将该文本添加到哈希,我声明了一个OnTabChangeListener,每次我更改选项卡时都会将待处理内容转储到其中。

这不是最出色的解决方案,但它是我工作的唯一一个......所以,如果有人有更好的解决方案,我将不胜感激。

4 个答案:

答案 0 :(得分:0)

可通过TabWidget访问标签:

yourTabHost.getTabWidget().getChildTabViewAt(position);

此代码返回任何选项卡的视图,甚至是非活动选项卡。

答案 1 :(得分:0)

首先使用tabHost.getChildAt(n)获取整个标签的视图,然后在标签视图中执行findViewById以应用更改。

示例:

View tabView = tabHost.getChildAt(0);
View myView = tabView.findViewById(R.id.myView);
myView.setVisibility(View.INVISIBLE);

答案 2 :(得分:0)

@nKn,感谢getTabContentView()。在我的情况下,它不返回选项卡,而是包含所有选项卡的FrameLayout,以下工作:

View tab = tabHost.getTabContentView().getChildAt(2);
ImageView image = (ImageView) tab.findViewById(R.id.image);

答案 3 :(得分:0)

可以通过TabWidget方法getChildTabViewAt(position)

访问TabHost子标签视图
    TabWidget tabWidget = tabHost.getTabWidget();

    for (int i = 0; i < tabWidget.getTabCount(); i++) {

        View parentView = tabWidget.getChildTabViewAt(i);

    }

可以通过以下方式访问选定的标签视图:

View parentView = tabHost.getCurrentTabView();