2011-11-23 67 views
0

我檢查了一些關於如何在android中構建TAB的在線教程,並看到了適合我需求的教程。我很高興實施它,它運作良好。當我希望每個單獨的選項卡推送到一個單獨的活動時,問題就出現了。我無法開始並打算傳遞控制權。我只給出了關於如何製作Tab的主代碼,因爲構建所需的xml在此問題中沒有任何功能。將意圖添加到Android中的TabActivity

下面是代碼:

public class Secondactivity extends TabActivity { 
private TabHost mTabHost; 
private void setupTabHost() { 
mTabHost = (TabHost) findViewById(android.R.id.tabhost); 
mTabHost.setup(); 
} 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
setupTabHost(); 
    mTabHost.getTabWidget().setDividerDrawable(se.copernicus.activity.R.drawable.tab_divider); 

    setupTab(new TextView(this), "Month"); 
    setupTab(new TextView(this), "Week"); 
    setupTab(new TextView(this), "Day"); 
} 
private void setupTab(final View view, final String tag) { 
View tabview = createTabView(mTabHost.getContext(), tag); 

TabSpec setContent = mTabHost.newTabSpec(tag).setIndicator(tabview) 
     .setContent(new TabContentFactory() { 
      public View createTabContent(String tag) { 
       return view; 
      } 
     }); 
mTabHost.addTab(setContent); 

} 

private static View createTabView(final Context context, final String text) { 
View view = LayoutInflater.from(context) 
     .inflate(R.layout.tabs_bg, null); 
TextView tv = (TextView) view.findViewById(R.id.tabsText); 
tv.setText(text); 
return view; 
    } 
} 

我如何開始一個新的意圖,這樣在單擊標籤時,它應該從SecondactivityWeekActivityDayActivity

回答

2
public class MainTabActivity extends TabActivity 

{ 
    private TabHost mTabHost; 

@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.program_guide_tab_activity_layout); 

    mTabHost = (TabHost) findViewById(android.R.id.tabhost); 
    setupTab(new TextView(this), getString(R.string.live)); 
    setupTab(new TextView(this), getString(R.string.guide)); 
    setupTab(new TextView(this), getString(R.string.remotes)); 
    setupTab(new TextView(this), getString(R.string.settings)); 

    mTabHost.setCurrentTabByTag(getString(R.string.live)); 
} 

private void setupTab(final View view, final String tag) 
{ 
    View tabview = createTabView(mTabHost.getContext(), tag); 

    if (tag.compareTo(getString(R.string.live)) == 0) 
    { 
     Intent intent = new Intent(getApplicationContext(), LiveActivity.class); 

     TabSpec setContent = mTabHost.newTabSpec(getString(R.string.live)).setIndicator(tabview).setContent(new TabHost.TabContentFactory() 
     { 

      public View createTabContent(String tag) 
      { 
       return view; 
      } 

     }); 

     setContent.setContent(intent); 
     mTabHost.addTab(setContent); 
    } 

    if (tag.compareTo(getString(R.string.guide)) == 0) 
    { 
     Intent intent = new Intent(getApplicationContext(), ProgramGuide.class); 

     TabSpec setContent = mTabHost.newTabSpec(getString(R.string.guide)).setIndicator(tabview).setContent(new TabHost.TabContentFactory() 
     { 

      public View createTabContent(String tag) 
      { 
       return view; 
      } 

     }); 

     setContent.setContent(intent); 
     mTabHost.addTab(setContent); 
    } 

    if (tag.compareTo(getString(R.string.remotes)) == 0) 
    { 
     Intent intent = new Intent(getApplicationContext(), RemoteMultiPanel.class); 

     TabSpec setContent = mTabHost.newTabSpec(getString(R.string.remotes)).setIndicator(tabview).setContent(new TabHost.TabContentFactory() 
     { 

      public View createTabContent(String tag) 
      { 
       return view; 
      } 

     }); 

     setContent.setContent(intent); 
     mTabHost.addTab(setContent); 
    } 

    if (tag.compareTo(getString(R.string.settings)) == 0) 
    { 
     Intent intent = new Intent(getApplicationContext(), SettingsMain.class); 

     TabSpec setContent = mTabHost.newTabSpec(getString(R.string.settings)).setIndicator(tabview).setContent(new TabHost.TabContentFactory() 
     { 

      public View createTabContent(String tag) 
      { 
       return view; 
      } 

     }); 

     setContent.setContent(intent); 
     mTabHost.addTab(setContent); 
    } 
} 

private static View createTabView(final Context context, final String text) 
{ 
    View view = LayoutInflater.from(context).inflate(R.layout.tabs_bg, null); 
    TextView tv = (TextView) view.findViewById(R.id.tabsText); 
    tv.setText(text); 

    int resId = 0; 
    ImageView iconImageView = (ImageView) view.findViewById(R.id.imageView1); 

    if (text.compareTo(context.getString(R.string.settings)) == 0) 
    { 
     resId = R.drawable.settings_icon; 
    } 
    else if (text.compareTo(context.getString(R.string.remotes)) == 0) 
    { 
     resId = R.drawable.remotes_icon; 
    } 
    else if (text.compareTo(context.getString(R.string.live)) == 0) 
    { 
     resId = R.drawable.live_icon; 
    } 
    else if (text.compareTo(context.getString(R.string.guide)) == 0) 
    { 
     resId = R.drawable.guide_icon; 
    } 

    iconImageView.setImageResource(resId); 

    return view; 
} 

}

退房的setupTap()方法: 它控制的4個可能的意圖的,所提出的活動: LiveActivity,ProgramGuide,....

+0

謝謝!現在測試它... – Vinoth

+0

告訴我它是否有幫助,或者如果您需要任何其他幫助 –

+0

不要忘記標記爲「解決」,如果它真的幫助你:) –