【问题标题】:Android Tab Help. How to set 2nd tab as default when app opens?Android 选项卡帮助。应用程序打开时如何将第二个选项卡设置为默认值?
【发布时间】:2013-04-11 03:10:25
【问题描述】:

我创建了一个包含 3 个标签的应用。该应用程序运行良好,但我希望在应用程序打开时选择并加载第二个选项卡。我该如何设置?

这是我的代码:

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.setCurrentTab(1);因为你的标签是 0-1-2
  • val tabs = findViewById(R.id.tabs) tabs.getTabAt(1)!!.select() in kotlin (1 可以是你喜欢的任何索引)

标签: android tabs android-tabhost


【解决方案1】:

如果你有碎片,会有一些困难。黑客是使选项卡选择延迟。请参阅 code in this answer

【讨论】:

    【解决方案2】:
    public override void OnResume() 
        {
            base.OnResume();
            tabHost.CurrentTab = 1; //index of the tab you want to set to default.
        }
    

    重写 OnResume() 并将 tabHost 的 CurrentTab 属性分配给您想要的选项卡的索引。

    【讨论】:

    • OnResume 方法中的 tabHost.setCurrentTab() 对我有用
    【解决方案3】:

    在您的 onCreate 中使用 tabHost.setCurrentTab(1);

    【讨论】:

      【解决方案4】:

      在tabHost中添加标签后,使用此方法设置当前标签

      tabHost.setCurrentTab(1);  // here pass the tab index its starting from 0
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-08-03
        • 1970-01-01
        • 2019-05-17
        • 2019-04-07
        • 2013-07-24
        • 2017-08-08
        • 2013-09-25
        • 1970-01-01
        相关资源
        最近更新 更多