开发者

Android: TabActivity which performs a check before populating the content view

I have a TabActivity class which uses Intents to populate the content view. Under certain conditions, I would like to intercept a tab selection event, put up a message dialog, suppress the selected Intent, and revert to the original tab selected.

I want the TabActivity content to remain Intent driven (rather than using Views).

I suspect this may require extension of the LocalActivityManager.

Has 开发者_JAVA技巧anyone ever accomplished this or done a similiar thing?

// simple example of current code:

TabHost tabHost = getTabHost();
TabSpec ts = tabHost.newTabSpec(tag);
ts.setIndicator(tabview);
ts.setContent(new Intent().setClass(this, AHome.class));
tabHost.addTab(ts);

Thanks!


I wouldn't look in the TabActivity for the answer (even Google staff admit that this API is broken). Here's what I do - in the target activity, I'd check this condition in the onCreate, if the condition is satisfied, continue, if no - activate the previous activity


After a little digging into Android's TabHost src, here's a fairly simple solution to the problem. It allows the tab button to be "touched" graphically yet still remain unselected and it prevents any processing of the selected tab (assuming that all the OnTabSelected listeners have been made aware).

Just Extend the TabHost class:

public class MyTabHost extends TabHost
{
    public MyTabHost(Context context)
    {
        super(context);
    }

    public MyTabHost(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }

    public void setCurrentTab(int index) 
    {
        // e.g. substitute ? with the tab index(s) for which to perform a check.  
        if (index == ?)
        {
            if (/* a block condition exists */)
            {
                // Perform any pre-checking before allowing final tab selection
                Toast.makeText(this.getContext(), "msg", Toast.LENGTH_SHORT).show();
                return;
            }
        }
        super.setCurrentTab(index);
    }
}

And then change your reference from TabHost to MyTabHost in the XML used for the TabActivity:

<com.hos.MyTabHost 
    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:id="@+id/llTest"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="0dp"
        >

    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="0dp" 
        android:layout_gravity="top"
        android:layout_weight="1"
        />

    <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:layout_gravity="bottom"            
        android:layout_weight="0"
        />

    </LinearLayout>

</com.hos.MyTabHost>

One other thing to remember is that if you use TabActivity.getTabHost() in the TabActivity, it will return a MyTabHost. e.g.:

MyTabHost mth = (MyTabHost)getTabHost();
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜