1

我有一个应用程序,其中第一页有三个按钮(例如 A、B、C 按钮)。当我单击这三个按钮中的任何一个时,它们将导航到相同的 tabActivity 页面。默认情况下,第一个选项卡被选中。但我想要的是,当我单击 A 按钮时,应该选择第一个选项卡。当我单击 B 按钮时,应该选择第二个选项卡。当我单击 C 按钮时,应该选择第三个选项卡。我不知道该怎么做。谁可以帮我这个事?

提前致谢...

4

2 回答 2

2

尝试这个,

点击按钮 A

tabHost.setCurrentTab(0); 

点击按钮 B

tabHost.setCurrentTab(1); 

点击按钮 C

tabHost.setCurrentTab(2);
于 2013-01-24T06:11:14.270 回答
1

在你的活动课上试试这个,在我的应用程序中工作得很好。

Resources res = getResources(); // Resource object to get Drawables
        TabHost tabHost = getTabHost();  // The activity TabHost
        TabHost.TabSpec spec;  // Resusable TabSpec for each tab 
        Intent intent;  // Reusable Intent for each tab

            // Create an Intent to launch an Activity for the tab (to be reused) 
           intent = new Intent().setClass(this, ArtistsActivity.class);
            // Initialize a TabSpec for each tab and add it to the TabHost
            spec = tabHost.newTabSpec("artists").setIndicator("Artists",
                              res.getDrawable(R.drawable.ic_tab_artists))
                          .setContent(intent);
            tabHost.addTab(spec);

            // Do the same for the other tabs 
           intent = new Intent().setClass(this, AlbumsActivity.class); 
           spec = tabHost.newTabSpec("albums").setIndicator("Albums", 
                             res.getDrawable(R.drawable.ic_tab_albums))
                          .setContent(intent);
            tabHost.addTab(spec);

            intent = new Intent().setClass(this, SongsActivity.class);
            spec = tabHost.newTabSpec("songs").setIndicator("Songs",
                              res.getDrawable(R.drawable.ic_tab_songs)).setContent(intent);
            tabHost.addTab(spec);   
        }

希望对你有帮助

于 2012-06-23T05:23:14.750 回答