[英]Android : How to set a current tab selected by clicking on each different button?
I have an app in which i have three buttons in the first page(For example say A, B, C buttons). 我有一个应用程序,其中我在第一页有三个按钮(例如说A,B,C按钮)。 When i click on any one of these three buttons they will navigate to same tabActivity page.
当我点击这三个按钮中的任何一个时,他们将导航到相同的tabActivity页面。 By default first tab is getting selected.
默认情况下,第一个选项卡被选中。 But what i want is, when i click on A button first tab should be get selected.
但我想要的是,当我点击A按钮时,第一个选项卡应该被选中。 when i click on B button second tab should be get selected.
当我点击B按钮时,应该选择第二个标签。 when i click on C button third tab should get selected.
当我点击C按钮时,第三个标签应该被选中。 I dont know how to do this.
我不知道该怎么做。 Can any one help me on this?
谁可以帮我这个事?
Thanks in advance... 提前致谢...
Try This, 尝试这个,
on clicking button A 点击按钮A.
tabHost.setCurrentTab(0);
on clicking button B 点击按钮B
tabHost.setCurrentTab(1);
on clicking button C 点击按钮C
tabHost.setCurrentTab(2);
in your activity class try this and in my app is working just fine. 在你的活动课中尝试这个,并在我的应用程序工作正常。
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);
}
Hope it helps you 希望它能帮到你
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.