开发者

change from a tab to another

i want to make an intent from a tab to another.. how can i do that? i only now how to make between activity's. and i need to go from a tab to another..

i have this code for the tabs

public class Main extends TabActivity

{

@Override
protected void onCreate(Bundle savedInstanceState)
{
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);



     Resources res = getResources(); // Resource object to get Drawables
        TabHost tabHost = getTabHost();  // The activity TabHost
        TabHost.TabSpec spec;  // Resusable TabSpec for each tab
        Intent i开发者_开发知识库ntent;  // Reusable Intent for each tab


        // Create an Intent to launch an Activity for the tab (to be reused)
        //intent = new Intent().setClass(this, Contas.class);
        Intent a = new Intent(Main.this, Contas.class);
        Intent b = new Intent(Main.this, Registros.class);
        Intent c = new Intent(Main.this, Relatorios.class);
        // Initialize a TabSpec for each tab and add it to the TabHost
        spec = tabHost.newTabSpec("Contas").setIndicator("Contas",
                          res.getDrawable(R.drawable.ic_tab_accounts))
                      .setContent(a.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
        tabHost.addTab(spec);


        // Do the same for the other tabs
        //intent = new Intent().setClass(this, Registros.class);
        spec = tabHost.newTabSpec("Registros").setIndicator("Registros",
                          res.getDrawable(R.drawable.ic_tab_registry))
                      .setContent(b.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
        tabHost.addTab(spec);

        //intent = new Intent().setClass(this, Relatorios.class);
        spec = tabHost.newTabSpec("Relatorios").setIndicator("Relatorios",
                          res.getDrawable(R.drawable.ic_tab_reports))
                      .setContent(c.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
        tabHost.addTab(spec);

        tabHost.setCurrentTab(0);


}

}


Rather then calling intent you can just set the desired tab selected. tabHost.setCurrentTab(index)


Rather then calling intent you can just set the desired tab selected. Use

intent.putExtra("tabIndex", index);

and call the activity. Now in the calling Activity's onCreate() or onResume() use

int index = getIntent().getExtraInt("tabIndex", -1);
if(index != -1)
    tabHost.setCurrentTab(index);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜