2011-03-28 76 views
0

我设置了一个与Google的选项卡教程非常相似的基本TabActivity。然而,在TabWidget上方,我有一个TextView,我想更新,并在每个子选项卡/子活动中触发某些事件(不一定在切换选项卡时)后更改文本。我意识到我可能无法在活动之间绑定事件,因此有关如何实现此目的的任何想法?设置TabActivity和儿童活动通过的服务?在儿童活动处于活动状态时,甚至可以在TabHost中将TextView放在这里甚至重新绘制?将子选项卡内容中的“事件”传递给父TabActivity

我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> 

回答

5

做一些很丑陋的解决方法后,终于想出了一个非常简单的解决方案。从儿童活动内部,我补充说:

protected void updateParentText(){ 
    MyTabActivity parent = (MyTabActivity) this.getParent(); 
    TextView tv = (TextView) parent.findViewById(R.id.textToUpdate); 
    tv.setText("New Text here"); 
}