将“事件”从子选项卡内容活动传递到父TabActivity

时间:2011-03-28 22:39:41

标签: android events tabactivity

我设置的基本TabActivity与Google的标签教程非常相似。但是,在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>

1 个答案:

答案 0 :(得分:5)

做了一些非常难看的变通办法之后,终于找到了一个非常简单的解决方案。在儿童活动中,我补充道:

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