简体   繁体   English

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

[英]Passing “events” from child tab content Activity to parent TabActivity

I setup a basic TabActivity very similar to Google's tab tutorial. 我设置了一个基本的TabActivity,与Google的标签教程非常相似。 However, above the TabWidget I have a TextView that I'd like to update and change the text after certain events get fired in each child tab/child activity (not necessarily when switching between tabs). 但是,在TabWidget上方,我有一个TextView,在每个子选项卡/子活动中触发某些事件后,我想更新和更改文本(在选项卡之间切换时不一定)。 I realize I may not be able to bind events between activities so any ideas on how to achieve this? 我意识到我可能无法在活动之间绑定事件,所以关于如何实现这一点的任何想法? Setup a service that both the TabActivity and children Activities communicate through? 设置TabActivity和子活动都通过其通信的服务吗? Is it even possible to get the TextView here in the TabHost to even redraw while a child activity is active? 在子活动处于活动状态时,是否甚至可以在TabHost中获得TextView,甚至可以重绘?

My TabActivity inflates the following view: 我的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>

After doing some very ugly workarounds, finally figured out a very simple solution. 经过一些非常丑陋的解决方法后,终于找到了一个非常简单的解决方案。 From within the child activity, I added: 在子活动中,我添加了:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM