【发布时间】:2018-11-04 20:19:34
【问题描述】:
在我在 Android Studio 中的 java 代码中,我有一个简单的选项卡结构,其结构如下:
<TabHost
android:id="@android:id/tabhost"
....>
<LinearLayout
....>
<TabWidget
android:id="@android:id/tabs"
....>
<TextView
...
android:text="tab0" />
<TextView
...
android:text="tab1" />
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
...>
<TextView
android:id="@+id/textView_0"
android:text="content0"
..../>
<TextView
android:id="@+id/textView_1"
android:text="content1"
..../>
</FrameLayout>
</LinearLayout>
</TabHost>
在我的代码的一部分中,我需要阅读 TextVeiw 内容。 虽然我可以使用以下代码阅读标签标题,但我找不到访问标签内容的方法(我可以在手机屏幕上看到内容
...
TabWidget tab_widgets = tabHost.getTabWidget();
FrameLayout tab_contents = tabHost.getTabContentView();
for (int index = 0; index < tab_widgets.getChildCount(); index++) {
//access tab's title: Works OK !!
View v_title =
tab_widgets.getChildAt(index).findViewById(android.R.id.title);
TextView tv_title = (TextView) v_title;
//access tab's content: NOT working ??
View v_content =
tab_contents.getChildAt(index).findViewById(android.R.id.title);
TextView tv_content = (TextView) v_content; // It is always NULL
}
对此有什么想法吗?如何阅读标签正文中的文本?
【问题讨论】:
-
显然,TextViews 可以使用它们的显式 id 直接访问。但是在这里,我想在循环中访问它们,例如“对于所有 TextViews ...做...”
标签: android android-studio tabs textview findviewbyid