0

我设置了一个与 Google 的标签教程非常相似的基本 TabActivity。但是,在 TabWidget 上方,我有一个 TextView,我想在每个子选项卡/子活动中触发某些事件后更新和更改文本(不一定在选项卡之间切换时)。我意识到我可能无法在活动之间绑定事件,所以关于如何实现这一点的任何想法?设置一个服务,TabActivity 和子活动都通过它进行通信?甚至有可能在子活动处于活动状态时让 TabHost 中的 TextView 甚至重绘?

我的 TabActivity 夸大了以下视图:

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:orientation="vertical"
    android:layout_width="fill_parent" android:layout_height="fill_parent">
    <TextView android:id="@+id/textToUpdate" android:text="Some text to update" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>

    <TabWidget android:id="@android:id/tabs"
        android:layout_width="fill_parent" android:layout_height="wrap_content" />
    <FrameLayout android:id="@android:id/tabcontent"
        android:layout_width="fill_parent" android:layout_height="fill_parent" />
</LinearLayout>

4

1 回答 1

5

在做了一些非常难看的解决方法之后,终于想出了一个非常简单的解决方案。在子活动中,我添加了:

protected void updateParentText(){
    MyTabActivity parent = (MyTabActivity) this.getParent();
    TextView tv = (TextView) parent.findViewById(R.id.textToUpdate);
    tv.setText("New Text here");
}
于 2011-05-08T14:41:07.380 回答