开发者

What is the equivalent of ActionScript 3's Event broadcasting in Android?

I have a custom title bar in an Android application that just has a simple TextView in it at the moment. I'm wondering what the best class/method to use to update the title from any activity would be? For example, I have a TabActivity that has this title bar in it and I need to change the title when one of the Tab Activities changes. I'd also want to change the title bar in other activities that aren't in a tab context.

I've looked at BroadcastReceivers and Intents, but I'm not really understanding the nomenclature, so I'm not sure what the equivalent of something like Actionscript's Event broadcasting is. I'd like my title bar class to listen for a "global" application event that recieves a String and update the title when it receives that event. How would I do that?

In another example, in iOS I'd use NSNotificationCenter and use postNotification to fire an "event". Then I'd have a view controller addObserver and listen for that event and respond to it. How is something like this accomplished within the Android framework?

For example's sake, I've got my "main" Activity:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <!-- here's my header-->
    <include layout="@layout/header"/>


    <TabHost
        android:id="@开发者_如何学Pythonandroid: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">
            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="fill_parent"
                android:layout_height="0dip"
                android:layout_weight="1"/>
            <TabWidget
                style="@style/TabWidget"
                android:id="@android:id/tabs"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"/>
        </LinearLayout>
    </TabHost>

And the header include has the text field I want to change:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    style="@style/Header">
    <TextView
        style="@style/HeaderTitle"
        android:text="@string/app_name"/>
</LinearLayout>

So, basically I'm looking for a way to pass up notifications to the header from any of the activities in the TabHost or other activities that have the header included.


I hesitate to call this an authoritative answer as I'm still learning Android development myself. Hence, this is an open community wiki. Hopefully some of these answers will lead you where you want to go.

If you've been playing around with the Android event listeners you might have noticed some code like this:

Button newButton = (Button) this.findViewById(R.id.headerbutton);
newButton.setText("click me");
newButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            globalHeader.setText("GOOOOGGGG");

        }
    });

OnClickListener is an interface impleneted by View that has a callback function onClick that is publicly exposed so that the internals of newButton can call it when it detects that it has been clicked. The objects have to pass themselves as listeners, unlike ActionScript where functions are first class members. That's the general basics of the Java event listener/dispatcher relationship. Where did I get the globalHeader variable? Like this:

final TextView globalHeader = (TextView) this.findViewById(R.id.TextView_GlobalHeader);     // TextView_GlobalHeader is just the id I assigned 
//to the header TextView that's used in the import statement.

Notice that it's declared final. This is because you cannot pass it to the event handler unless the variable containing the TextView is final.

So I have not yet found a global EventDispatch type hub in the android SDK. I've wondered whether such a thing exists myself, and if it doesn't, then is this to head off memory leaks? Or maybe it is because Java is so strict that there's precious little you can assume that any given listener will be able to do? I think the best you'd be able to do in that case is just write a very generic OnGlobalEvent interface with, for example, a respondToMessage(String pMessage) function and then have the global dispatch hub iterate through all its listeners, calling respondToMessage(messsageType);

But here's hoping that someone who actually knows what he or she is talking about comes along and shows me wrong.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜