2010-06-25 79 views
6

我有一個TabActivity,其中包含4個活動。我的代碼將第二個選項卡作爲當前選項卡:如何防止在TabActivity的第一個選項卡上啓動活動?

public class MyTabActivity extends TabActivity { 
    TabHost tabHost = getTabHost(); 
    TabHost.TabSpec spec; // Resusable TabSpec for each tab 
    Intent intent; // Reusable Intent for each tab 

    TextView tabView; 

    // Create an Intent to launch an Activity for the tab (to be reused) 
    intent = new Intent().setClass(this, Activity1.class); 

    spec = tabHost.newTabSpec("Tab 1"); 
    spec.setContent(intent); 
    tabView = (TextView) inflater.inflate(R.layout.ff_tab_indicator, null); 
    tabView.setText("Tab 1"); 
    spec.setIndicator(tabView); 
    tabHost.addTab(spec); 

    intent = new Intent().setClass(this, Activity2.class); 
    spec = tabHost.newTabSpec("Tab 2"); 
    spec.setContent(intent); 
    tabView = (TextView) inflater.inflate(R.layout.ff_tab_indicator, null); 
    tabView.setText("Tab 2"); 
    spec.setIndicator(tabView); 
    tabHost.addTab(spec); 

    intent = new Intent().setClass(this, Activity3.class); 
    spec = tabHost.newTabSpec("Tab 3"); 
    spec.setContent(intent); 
    tabView = (TextView) inflater.inflate(R.layout.ff_tab_indicator, null); 
    tabView.setText("Tab 3"); 
    spec.setIndicator(tabView); 
    tabHost.addTab(spec); 

    intent = new Intent().setClass(this, Activity4.class); 
    spec = tabHost.newTabSpec("Tab 4"); 
    spec.setContent(intent); 
    tabView = (TextView) inflater.inflate(R.layout.ff_tab_indicator, null); 
    tabView.setText("Tab 4"); 
    spec.setIndicator(tabView); 
    tabHost.addTab(spec); 

    tabHost.setCurrentTab(1); 
} 

的問題是,在MyTabActivity啓動時,它開始在第一個選項卡均具有活性,並在第二個選項卡的活動。我只是希望它在第二個選項卡中啓動活動,因爲它被設置爲當前選項卡。我該怎麼辦?

謝謝。

+1

@kknight:除了不使用活動選項卡的內容? – CommonsWare 2010-06-25 22:44:20

+0

@kknight:你想做什麼? – Macarse 2010-06-26 21:45:49

+0

我只想將選項卡2設置爲默認選項卡。當選項卡活動開始時,只有選項卡2中的活動開始。目前,如果我將選項卡2設置爲默認當前選項卡,則Android將在選項卡1中啓動活動並在選項卡2中啓動活動。 – user256239 2010-06-26 22:28:19

回答

1

如何重新排列標籤,使默認成爲第一個?

+0

確實。這是一個解決方案。不知道每次應用程序啓動時,我都想改變UI佈局的順序。有點混亂的XP IMO。 – typeoneerror 2011-02-20 08:26:18

3

試試這個:

tabHost.setCurrentTab(0); 
相關問題