始终先打开标签然后打开其他标签

时间:2013-05-27 02:09:53

标签: android android-activity tabs

我有第三个标签的标签活动

TabMainActivity

public class TabMainActivity extends TabActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    TabHost tabHost = getTabHost();

    Intent it=getIntent();
    int num_tab=it.getIntExtra("num_tab", 0);
    // Tab1
    TabSpec photospec = tabHost.newTabSpec("Tab1");
    // setting Title and Icon for the Tab
    photospec.setIndicator("Tab1", getResources().getDrawable(R.drawable.icon_photos_tab));
    Intent photosIntent = new Intent(this, PhotosActivity.class);
    photospec.setContent(photosIntent);

    // Tab2
    TabSpec songspec = tabHost.newTabSpec("Tab2");       
    songspec.setIndicator("Songs", getResources().getDrawable(R.drawable.icon_songs_tab));
    Intent songsIntent = new Intent(this, SongsActivity.class);
    songspec.setContent(songsIntent);

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

    // Adding all TabSpec to TabHost
    tabHost.addTab(photospec); // Adding photos tab
    tabHost.addTab(songspec); // Adding songs tab
    tabHost.addTab(videospec); // Adding videos tab

    tabHost.setCurrentTab(num_tab);
}

}

Tab1:Activity1.java

public class Activity1 extends Activity {

@Override
public void onCreate(Bundle circle)
{
    super.onCreate(circle);
    setContentView(R.layout.layout1);
    Toast.makeText(getBaseContext(), "This is tab1",Toast.LENGTH_LONG).show();
}

}

Tab2:Activity2.java

public class Activity2 extends Activity {

@Override
public void onCreate(Bundle circle)
{
    super.onCreate(circle);
    setContentView(R.layout.layout1);       
}

}

Tab3:Activity3.java

public class Activity3 extends Activity {

@Override
public void onCreate(Bundle circle)
{
    super.onCreate(circle);
    setContentView(R.layout.layout1);
            Button button3=(Button)findViewbyId(R.id.button3);
            button3.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Activity3.this.finish();
            Intent it=new Intent(Activity3.this,TabMainActivity.class);
            it.putExtra("num_tab",2);
            startActivity(it);
        }
    });
}

}

现在,如果在tab3中我点击button3 ..它总是在tab1中显示操作代码(显示吐司作为代码)..然后再打开tab3。我只想再打开tab3但不要在tab1中操作代码。 我该怎么办?

1 个答案:

答案 0 :(得分:0)

您可以尝试在“prepare()”中添加tab而不是“onCreate()”,但仍然可以在“onCreate()”中添加setCurrentTab。