Android:在标签改变时开始活动



我在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
}
}

这是我的tab .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>

你错过了th.setOnTabChangeListener(this);

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

最新更新