Android 选项卡帮助.应用程序打开时如何将第二个选项卡设置为默认值? [英] Android Tab Help. How to set 2nd tab as default when app opens?
本文介绍了Android 选项卡帮助.应用程序打开时如何将第二个选项卡设置为默认值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我创建了一个包含 3 个标签的应用.该应用程序运行良好,但我希望在应用程序打开时选择并加载第二个选项卡.我该如何设置?
I have created an app with 3 tabs. The app works fine but I want the 2nd tab to be selected and loaded when the app opens. How can I set that?
这是我的代码:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabHost tabHost = getTabHost();
// Tab for Home
TabSpec homespec = tabHost.newTabSpec("Home");
// setting Title and Icon for the Tab
homespec.setIndicator("Home",getResources().getDrawable(R.drawable.icons_home_tab));
Intent photosIntent = new Intent(this, HomeActivity.class);
homespec.setContent(photosIntent);
// Tab for Child
TabSpec childspec = tabHost.newTabSpec("Child");
childspec.setIndicator("Child",getResources().getDrawable(R.drawable.icons_child_tab));
Intent homeIntent = new Intent(this, ChildActivity.class);
childspec.setContent(homeIntent);
// Tab for Account
TabSpec accspec = tabHost.newTabSpec("Account");
accspec.setIndicator("Account",getResources().getDrawable(R.drawable.icons_account_tab));
Intent accIntent = new Intent(this, AccountActivity.class);
accspec.setContent(accIntent);
// Adding all TabSpec to TabHost
tabHost.addTab(homespec); // Adding home tab
tabHost.addTab(childspec); // Adding child tab
tabHost.addTab(accspec); //Adding account tab
}
推荐答案
在tabHost中添加tab后,使用该方法设置当前tab
Use this method to set the current tab after you added tab in tabHost
tabHost.setCurrentTab(1); // here pass the tab index its starting from 0
这篇关于Android 选项卡帮助.应用程序打开时如何将第二个选项卡设置为默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文