0

我在java中有以下类。我的目的是设计一个标签主机,并在显示第一个标签时启动一个活动。我还希望在单击另一个选项卡时启动不同的活动。我已经实现了 onTabChanged 方法,但它似乎不起作用。你能帮助我吗?

这是我的课:

public class Tabs extends Activity implements OnTabChangeListener{

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tabs);
    TabHost th = (TabHost) findViewById(R.id.tabhost);
    th.setup();
    TabSpec profile = th.newTabSpec("tag1");
    profile.setContent(R.id.tab1);
    profile.setIndicator("", getResources().getDrawable(R.drawable.profile_tab));

    TabSpec matches = th.newTabSpec("tag2");
    matches.setContent(R.id.tab2);
    matches.setIndicator("", getResources().getDrawable(R.drawable.matches_tab));

    TabSpec friends = th.newTabSpec("tag3");
    friends.setContent(R.id.tab3);
    friends.setIndicator("", getResources().getDrawable(R.drawable.friends_tab));

    th.addTab(profile);
    th.addTab(matches);
    th.addTab(friends);
}

@Override
public void onTabChanged(String arg0) {
    // TODO Auto-generated method stub
}

}

这是我的 tabs.xml 布局文件:

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

<TabHost
    android:id="@+id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </TabWidget>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <LinearLayout
                android:id="@+id/tab1"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
                <include layout="@layout/activity_main"/>
            </LinearLayout>

            <LinearLayout
                android:id="@+id/tab2"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
                <include layout="@layout/matches_page"/>
            </LinearLayout>

            <LinearLayout
                android:id="@+id/tab3"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
            </LinearLayout>
        </FrameLayout>
    </LinearLayout>
</TabHost>

</LinearLayout>
4

1 回答 1

0

你想念th.setOnTabChangeListener(this);

您必须告知必须调用接口TabHost的哪个实现OnTabChangeListener

于 2013-05-19T14:11:20.633 回答