简体   繁体   English

Android系统。 如何更改选项卡中的活动

[英]Android. How to change Activity within a Tab

Following situation: I have TabActivity with eg three tabs, TabA, TabB, TabC. 以下情况:我有TabActivity,例如三个选项卡,TabA,TabB,TabC。

There are a button in activity (Act_C_1) of TabC. TabC的活动(Act_C_1)中有一个按钮。 So if the user clicks on that button, another activity (Act_C_2) should occur in TabC. 因此,如果用户单击该按钮,则TabC中应发生另一个活动(Act_C_2)。

I thank you in advance for any suggestions / or ideas. 我提前感谢您提出任何建议/想法。

Mur 穆尔

UPD: UPD:

Here is my code 这是我的代码

TabActivity with three Activities: TabActivity有三个活动:

public class TabScreen extends TabActivity
{
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.tab_menu);

        TabHost tabHost = getTabHost();  // The activity TabHost
        TabHost.TabSpec spec;  // Resusable TabSpec for each tab
        Intent intent;  // Reusable Intent for each tab

        intent = new Intent().setClass(this, SecondActivity.class);

        // Initialize a TabSpec for each tab and add it to the TabHost
        spec = tabHost.newTabSpec("tab_1").setIndicator("Tab1",null).setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, ThirdActivity.class);
        // Initialize a TabSpec for each tab and add it to the TabHost
        spec = tabHost.newTabSpec("tab_2").setIndicator("Tab2",null).setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, FourthActivity.class);
        spec = tabHost.newTabSpec("tab_3").setIndicator("Tab3",null).setContent(intent);
        tabHost.addTab(spec);
    }

}

Activity 'Act_C_1' or FourthActivity.java: 活动'Act_C_1'或FourthActivity.java:

public class FourthActivity extends Activity implements OnClickListener
{
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fourth);

        Button BtnWeiter = (Button)findViewById(R.id.BtnWeiter);
        BtnWeiter.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) 
    {                    
        // I also tried to use LocalActivityManager
        // TabActivity parentTabActivity = (TabActivity) getParent();            
        // LocalActivityManager manager = parentTabActivity.getLocalActivityManager();
        // manager.destroyActivity("tab_3", true);
        // manager.startActivity("tab_3", new Intent(this, FourthActivity.class));
        finish();
        startActivity(new Intent(this, FourthActivity.class));            
    }        
}

The Activities in the tab can be switched in the following manner. 可以按以下方式切换选项卡中的活动。

First Let us understand the flow: 首先让我们了解一下流程:

  1. We have in a Tab host , activity (say a list) from which we need to go to the next Activity (say details for the clicked item) under the same tab. 我们在Tab主机中有一个活动(比如一个列表),我们需要在同一个标​​签下转到下一个Activity(说明被点击项目的详细信息)。 For this we can use the concept of replacing the activity.Also setting the flags for the tab selected and other for knowing that details are being shown now 为此,我们可以使用替换活动的概念。还可以设置所选选项卡的标志以及其他用于了解现在正在显示详细信息的标记

  2. When we press back we should get the previous activity under the same tab.For this instead of again replacing the activity we can refresh the tab while using the particular flag for tab which was selected. 当我们按下时,我们应该在相同的选项卡下获取之前的活动。为此,我们可以在使用选中的选项卡的特定标志时刷新选项卡而不是再次替换活动。 Also if flag for show details is true we'll go the the list in the same tab or else we will go the activity before the tabwidget (normal use of onBackPressed) 此外,如果显示详细信息的标志为true,我们将在同一个选项卡中列出该列表,否则我们将在tabwidget之前进行活动(正常使用onBackPressed)

The code can be as follows.. 代码可以如下..

  1. For going from list to details... 从列表到详细信息......

(This can be in the onClickListener) (这可以在onClickListener中)

private OnClickListener textListener = new OnClickListener() {

    @Override
    public void onClick(View v) {
        Constants.SHOW_DETAILS = true;
        Intent intent = new Intent(context, DetailsActivity.class);
        replaceContentView("activity3", intent);
        }
};

public void replaceContentView(String id, Intent newIntent) {
    View view = ((ActivityGroup) context)
            .getLocalActivityManager()
            .startActivity(id,
                    newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
            .getDecorView();
    ((Activity) context).setContentView(view);

}
  1. When back pressed is done we override on BackPressed in each of the Activity under the tab to go to the list again from the details screen 当后退完成后,我们覆盖选项卡下每个Activity中的BackPressed,再次从详细信息屏幕转到列表

     @Override public void onBackPressed() { // TODO Auto-generated method stub super.onBackPressed(); if (MathHelper.SHOW_DETAILS) { Log.e("back", "pressed accepted"); Constants.LIST_ACTIVITY = 1; Constants.SHOW_DETAILS = false; Intent intent = new Intent(this, Tab_widget.class); startActivity(intent); finish(); } } 

The most important part here is Constants.LIST_ACTIVITY = 1; 这里最重要的部分是Constants.LIST_ACTIVITY = 1; it indicates which tab we are in. so the corresponding activities will have its value as 0,1,2...etc 它指示我们所在的选项卡。因此相应的活动将其值设为0,1,2 ...等

Again to load the correct list (Activty) when the tab activity is refreshed we have to include this in the TabWidget onCreate after the creation of the tabs 再次在刷新选项卡活动时加载正确的列表(Activty),我们必须在创建选项卡后将其包含在TabWidget onCreate中

tabHost.setCurrentTab(Constants.LIST_ACTIVITY);

Just use Intent.FLAG_ACTIVITY_CLEAR_TOP flag to replace an activity for necessary tab. 只需使用Intent.FLAG_ACTIVITY_CLEAR_TOP标志替换必要选项卡的活动。

intent = new Intent().setClass(this, YourActivity.class);
spec   = tabHost.newTabSpec("tab1").setIndicator("Tab1",null)
        .setContent(intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
tabHost.addTab(spec);

Just add this for all tabs: 只需为所有标签添加:

yourTabHost.getChildAt(0).setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        getTabHost().setCurrentTab(0);
    }
});

Handle the onClick event for button 处理onClick事件按钮

finish() the activity Add startActivity method to start another activity. finish()活动添加startActivity方法以启动另一个活动。

Thanks 谢谢

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

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