简体   繁体   English

如何在Android Tabhost中更改标签内容?

[英]How to change tab content in android Tabhost?

I am having 3 Tabs Tab1,tab2, Tab3. 我有3个标签Tab1,tab2,Tab3。 In tab1 I am loading page1.java. 在tab1中,我正在加载page1.java。 In page1 there is a Next button. 在第1页中,有一个下一步按钮。 I need to change to page2.java while tap on the next button on Page1.java. 在点击Page1.java的下一个按钮时,我需要更改为page2.java。 I need to change only the content. 我只需要更改内容。 I need the tabs there as permanent. 我需要在那里的标签作为永久标签。 Is it possible. 可能吗。 I am doing like this on next button click, 我在下次单击按钮时就这样做,

Intent nextPageIntent = new Intent(context, Page2.class);
startActivity(nextPageIntent);

Please help me. 请帮我。

UPDATE: 更新:

For that reason you need a ActivityGroup within the Tab where you want to change the Activity. 因此,您需要在Tab内的一个ActivityGroup中,您要在其中更改Activity。

    TabHost tabHost = getTabHost();

    tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("Page1").setContent(
            new Intent(this, Page1.class)));
    tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("Page2").setContent(
            new Intent(this, Page2.class)));
    tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("Page3").setContent(
            new Intent(this, Page3.class)));

The Page1 takes care which Activity is shown in the first tab. Page1负责在第一个选项卡中显示哪个活动。 With setContentView you can bring another View to the front after an action is triggered. 使用setContentView您可以在触发操作后将另一个View置于最前面。

    Intent intent = new Intent(context, Page2.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    View view = Page1.group.getLocalActivityManager().
                startActivity(intent).getPage2View();

    Page1.group.setContentView(view);

You need to tell the ActivityGroup which view is to be on top and set the appropriate Flag, so that the View will be on top. 您需要告诉ActivityGroup哪个视图在顶部,并设置适当的Flag,以便该视图在顶部。

Of course now you have to keep track which activity is in front, how they behave and what happens if the back button is pressed. 当然,现在您必须跟踪哪些活动在前面,它们的行为以及如果按下后退按钮会发生什么。 But that gives you room for customizing the behavior. 但这为您提供了自定义行为的空间。

More from: use-android-activitygroup-within-tabhost-to-show-different-activity . 更多内容来自: 在tabhost中使用Android-activitygroup-to-show-different-activity

tabHost.setCurrentTab(2)应该这样做。

You can try replacing fragment for that. 您可以尝试为此替换片段

So keep all your pages (for one tab) in separate fragment. 因此,将所有页面(用于一个选项卡)放在单独的片段中。 Load fragment 1 - ie Page 1 initially, and on click of Next Button, replace fragment like this: 加载片段1-即最初的页面1,然后单击“下一步”按钮,替换片段,如下所示:

nextBtn.setOnClickListener(new OnClickListener(){

    @Override
    public void onClick(View arg0) {

        Page2Fragment fragment = (Page2Fragment)myFragmentManager.findFragmentByTag(TAG_1);

        if (fragment == null) {

            //load page 2 in fragment
            FragmentTransaction fragmentTransaction = myFragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.maincontainer, myFragment1, TAG_1);
            fragmentTransaction.commit();
        }
    }
});

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM