安卓.如何更改选项卡内的活动 [英] Android. How to change Activity within a Tab

查看:24
本文介绍了安卓.如何更改选项卡内的活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下情况:我有 TabActivity,例如三个标签,TabA、TabB、TabC.

Following situation: I have TabActivity with e.g. three tabs, TabA, TabB, TabC.

TabC 的活动 (Act_C_1) 中有一个按钮.因此,如果用户单击该按钮,则 TabC 中应发生另一个活动 (Act_C_2).

There are a button in activity (Act_C_1) of TabC. So if the user clicks on that button, another activity (Act_C_2) should occur in TabC.

我预先感谢您的任何建议/或想法.

I thank you in advance for any suggestions / or ideas.

UPD:

这是我的代码

具有三个活动的 TabActivity:

TabActivity with three Activities:

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);
    }

}

活动Act_C_1"或FourthActivity.java:

Activity 'Act_C_1' or 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));            
    }        
}

推荐答案

选项卡中的Activity可以通过以下方式切换.

The Activities in the tab can be switched in the following manner.

首先让我们了解一下流程:

First Let us understand the flow:

  1. 我们在 Tab 主机中有一个活动(比如一个列表),我们需要从中转到同一选项卡下的下一个活动(比如点击项目的详细信息).为此,我们可以使用替换活动的概念.还为所选选项卡设置标志,以及其他了解现在正在显示详细信息的标志

  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. 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

当我们按下返回键时,我们应该在同一选项卡下获得上一个活动.为此,我们可以在使用选定选项卡的特定标志的同时刷新选项卡,而不是再次替换活动.此外,如果显示详细信息的标志为真,我们将进入同一选项卡中的列表,否则我们将进入 tabwidget 之前的活动(正常使用 onBackPressed)

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)

代码可以如下..

  1. 从列表到详细信息...

(这个可以在onClickListener中)

(This can be in the 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. 当回按完成后,我们在选项卡下的每个活动中覆盖 BackPressed 以再次从详细信息屏幕转到列表

  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

@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();
  }
 }

这里最重要的部分是Constants.LIST_ACTIVITY = 1;它指示我们在哪个选项卡中.因此相应的活动的值为 0,1,2...等

The most important part here is Constants.LIST_ACTIVITY = 1; it indicates which tab we are in. so the corresponding activities will have its value as 0,1,2...etc

为了在选项卡活动刷新时再次加载正确的列表(活动),我们必须在创建选项卡后将其包含在 TabWidget onCreate 中

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

tabHost.setCurrentTab(Constants.LIST_ACTIVITY);

这篇关于安卓.如何更改选项卡内的活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆