【问题标题】:Always open tab first then open other tabs始终先打开标签,然后再打开其他标签
【发布时间】:2013-05-27 02:09:52
【问题描述】:

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

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 中操作代码(将 toast 显示为代码)..然后再次打开 tab3。我只想再次打开 tab3 但不要在 tab1 中操作代码.. 我该怎么办?

【问题讨论】:

    标签: android android-activity tabs


    【解决方案1】:

    您可以尝试在“prepare()”中添加选项卡而不是“onCreate()”,但仍然在“onCreate()”中设置CurrentTab。

    【讨论】:

      猜你喜欢
      • 2017-02-15
      • 2022-11-18
      • 2011-12-21
      • 1970-01-01
      • 1970-01-01
      • 2011-09-19
      • 2017-07-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多