2013-05-19 42 views
0

我在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> 
+0

「看起來不是什麼」工作「的意思? – Blackbelt

+0

我試圖把Log.i放在onTabChanged裏面,但是當我改變標籤時,沒有任何東西顯示在我的logcat –

回答

0

你錯過th.setOnTabChangeListener(this);

您必須告知TabHost其實現OnTabChangeListener接口必須致電

+0

哦哇我不能相信我忘了那個。我應該休息一下lol謝謝 –

+0

如果有幫助,請將答案標記爲已接受幫助同一問題的人 – Blackbelt

+0

是的。當我在onTabChanged中啓動一個活動時,活動開始,但標籤不再存在。你知道如何使活動在其標籤內開始嗎? –

相關問題