Android系统。如何在标签内更改活动 [英] Android. How to change Activity within a Tab

查看:112
本文介绍了Android系统。如何在标签内更改活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下情况: 我有TabActivity与例如三个选项卡,塔巴,塔布,TABC。

有活性TABC的(Act_C_1)按钮。因此,如果用户点击该按钮,另一个活动(Act_C_2)应该发生在TABC。

我感谢你在前进的任何建议/或想法。

穆尔

UPD:

下面是我的code

TabActivity有三个活动:

 公共类TabScreen扩展TabActivity
{
    公共无效的onCreate(包savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        的setContentView(R.layout.tab_menu);

        TabHost tabHost = getTabHost(); //活动TabHost
        TabHost.TabSpec规范; // Resusable则tabspec为每个标签
        意向意图; //可重复使用意向为每个标签

        意图=新的意图()setClass(这一点,SecondActivity.class)。

        //初始化则tabspec为每个标签,并把它添加到TabHost
        规格= tabHost.newTabSpec(tab_1中)setIndicator(TAB1,NULL).setContent(意向)。
        tabHost.addTab(规范);

        意图=新的意图()setClass(这一点,ThirdActivity.class)。
        //初始化则tabspec为每个标签,并把它添加到TabHost
        规格= tabHost.newTabSpec(tab_2)setIndicator(TAB2,NULL).setContent(意向)。
        tabHost.addTab(规范);

        意图=新的意图()setClass(这一点,FourthActivity.class)。
        规格= tabHost.newTabSpec(tab_3)setIndicator(TAB3,NULL).setContent(意向)。
        tabHost.addTab(规范);
    }

}
 

活动Act_C_1或FourthActivity.java:

 公共类FourthActivity扩展活动实现OnClickListener
{
    公共无效的onCreate(包savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.fourth);

        按钮BtnWeiter =(按钮)findViewById(R.id.BtnWeiter);
        BtnWeiter.setOnClickListener(本);
    }

    @覆盖
    公共无效的onClick(视图v)
    {
        //我还试图用LocalActivityManager
        // TabActivity parentTabActivity =(TabActivity)的getParent();
        // LocalActivityManager经理= parentTabActivity.getLocalActivityManager();
        // manager.destroyActivity(tab_3,真正的);
        // manager.startActivity(tab_3,新的意向(这一点,FourthActivity.class));
        完();
        startActivity(新意图(这一点,FourthActivity.class));
    }
}
 

解决方案

该活动在该选项卡可以通过以下方式进行切换。

首先让我们来了解流程:

  1. 我们在制表主机,活动(比如列表)从中我们需要去到下一个活动(说的详细点的点击项)相同的选项卡下。为此,我们可以使用替换activity.Also设置标志选择的选项卡的概念,另一个是知道的细节现在正在显示

  2. 当我们preSS回我们应该得到下相同tab.For的previous这个活动,而不是再次更换,我们可以同时使用其中被选定为标签的特定标志刷新选项卡活动。此外,如果标志显示详细信息是真实的,我们会去的名单在同一个标​​签,否则,我们将在tabwidget前走活动(正常使用onBack pressed的)

在code可以如下..

  1. 对于从名单将细节...

(这可以是在onClickListener)

 私人OnClickListener的TextListener =新OnClickListener(){

    @覆盖
    公共无效的onClick(视图v){
        Constants.SHOW_DETAILS = TRUE;
        意向意图=新的意图(背景下,DetailsActivity.class);
        replaceContentView(activity3,意图);
        }
};

公共无效replaceContentView(字符型,意图newIntent){
    查看查看=((的ActivityGroup)上下文)
            .getLocalActivityManager()
            .startActivity(ID,
                    newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
            .getDecorView();
    ((活动)范围内).setContentView(视图);

}
 

  1. 在回pressed做是我们覆盖的选项卡下pssed在每个活动的返回$ P $转到列表中再次从细节屏幕

      @覆盖
      公共无效onBack pressed(){
    // TODO自动生成方法存根
    super.onBack pressed();
    如果(MathHelper.SHOW_DETAILS){
        Log.e(后退,pressed接受);
        Constants.LIST_ACTIVITY = 1;
        Constants.SHOW_DETAILS = FALSE;
        意向意图=新的意图(这一点,Tab_widget.class);
        startActivity(意向);
        完();
      }
     }
     

最重要的部分在这里 Constants.LIST_ACTIVITY = 1;它表明我们是在哪个标签。所以相应的活动,将其值设为0,1,2 ...等

再次加载正确的列表(Activty)当标签活动被刷新,我们有创造的选项卡后,包括这在TabWidget的onCreate

  tabHost.setCurrentTab(Constants.LIST_ACTIVITY);
 

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

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.

Mur

UPD:

Here is my code

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

}

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

解决方案

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

The code can be as follows..

  1. For going from list to details...

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

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

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

这篇关于Android系统。如何在标签内更改活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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