我在java中擁有以下類。我的目的是設計一個tabhost,並在顯示第一個選項卡時啓動一個活動。另外我想要在另一個選項卡被點擊時啓動不同的活動。 我已經實現了onTabChanged方法,但它似乎並沒有工作。你可以幫我嗎?Android:在選項卡更改上啓動活動
這裏是我的類:
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>
「看起來不是什麼」工作「的意思? – Blackbelt
我試圖把Log.i放在onTabChanged裏面,但是當我改變標籤時,沒有任何東西顯示在我的logcat –