TabActivity和子选项卡

时间:2012-09-26 09:45:12

标签: android android-layout android-intent android-tabhost

我有一个tabActivity类。

class MainActivity extends TabActivity {
    TabHost tabHost = getTabHost();

TabSpec tabOne= tabHost.newTabSpec("Tab One");
tabOne.setIndicator("Tab One", getResources().getDrawable(R.drawable.tab1));
Intent tabOneIntent = new Intent(this, PhotosActivity.class);
tabOne.setContent(tabOneIntent);

TabSpec tabTwo= tabHost.newTabSpec("Tab Two");
tabTwo.setIndicator("Tab Two", getResources().getDrawable(R.drawable.tab2));
Intent tabTwoIntent = new Intent(this, TabTwo.class);
tabTwo.setContent(videosIntent);

tabHost.addTab(tabOne);
tabHost.addTab(tabTwo);
}

其中一个标签是

class TabTwo extends Acitity implements OnClickListener{
    public void onClick(View view) {
       //open a new activity within the tab  
    }
}

假活动是

class DummyActivity extends Activity {
}

当我点击tab 2时,会调用TabTwo类。当我从类中单击一个按钮时,我想在选项卡中打开DummyActivity。现在使用的代码是

context.startActivity(intentObject);

这将在绝对新窗口中打开新活动。如何从选项卡中打开DummyActivity?

提前致谢!

@all感谢您的回复。我使用ActivityGroup和子活动解决了这个问题。

3 个答案:

答案 0 :(得分:0)

您不得使用context.startActivity(intentObject);

看这里:

    TabHost tabHost = getTabHost();

    TabSpec photospec = tabHost.newTabSpec("Photos");
    photospec.setIndicator("Photos", getResources().getDrawable(R.drawable.icon1));
    Intent photosIntent = new Intent(this, PhotosActivity.class);
    photospec.setContent(photosIntent);

    TabSpec videospec = tabHost.newTabSpec("Videos");
    videospec.setIndicator("Videos", getResources().getDrawable(R.drawable.icon2));
    Intent videosIntent = new Intent(this, VideosActivity.class);
    videospec.setContent(videosIntent);

    tabHost.addTab(photospec);
    tabHost.addTab(videospec);

来自http://www.androidhive.info/2011/08/android-tab-layout-tutorial/

您必须使用TabHostTabSpecIntent作为标签。然后TabHost.addTab(TabSpec)

答案 1 :(得分:0)

  1. 它看起来不像通常的Android应用体验。做这些事情要非常小心。

  2. 从3.0开始,最好使用Fragments。如果要支持早期设备 - 请使用兼容性库。

答案 2 :(得分:0)

像第一个选项卡一样使用

 public class TabsActivity extends TabActivity {

      TabSpec spec1 = tabHost.newTabSpec("tabone");
    spec1.setIndicator(createTabView(tabHost.getContext(), "tabone", R.drawable.tab_home));
    Intent inte = new Intent(TabsActivity.this, PhotosActivity.class);
    inte.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    spec1.setContent(inte);


    TabSpec spec2 = tabHost.newTabSpec("tabtwo");
    spec2.setIndicator(createTabView(tabHost.getContext(), "tabtwo", R.drawable.tab_account));
    Intent _int = new Intent(TabsActivity.this, TabTwoActivity.class);
    _int.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    spec2.setContent(_int);

    tabHost.addTab(spec1);
    tabHost.addTab(spec2);

第二个标签

        public class TabTwoActivity extends TabActivity {

       TabSpec spec1 = tabHostacc.newTabSpec("tabone");
    spec1.setIndicator(createTabView(tabHostacc.getContext(), "tabone", R.drawable.tab_home));
    Intent inte = new Intent(TabTwoActivity .this,  yourfirstActivity.class);
    inte.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    spec1.setContent(inte);      

    TabSpec spec2 = tabHostacc.newTabSpec("tabtwo");
    spec2.setIndicator(createTabView(tabHostacc.getContext(), "tabtwo", R.drawable.tab_account));
    Intent _int = new Intent(TabTwoActivity .this, yoursecondActivity.class);
    _int.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    spec2.setContent(_int);      

    tabHostacc.addTab(spec1);
    tabHostacc.addTab(spec2);

demoActivityes

    class yourfirstActivity extends Activity {
     }

     class yoursecondActivityextends Activity {
     }