如何禁用活动中的选项卡?

How to disable tabs from the activities?

我有 2 个活动, 第一个 activity 包含两个按钮,这些按钮转到第二个 activity。

第二个 activity 由 2 个选项卡主机组成。

我想通过第一个 activity 的按钮启用第二个 tabhost。我该怎么做,请帮助我,谢谢...^_^

第一个Activity代码:

public void OnClickRanoNews(View v)
{
    if (v.getId() == R.id.btnRanoNews) 
    {
        Intent i = new Intent(this, RanoNews.class);
        startActivity(i);
    }

}

public void OnClickRanoNews2(View v)
{
        if(v.getId()== R.id.btnRanoNews2)
        {
            Intent i = new Intent(this, RanoNews.class);
            startActivity(i);
            //I want to disable the second tabhost in this button
        }
}

第二个Activity代码:

tabHost = (TabHost)findViewById(R.id.tabHost);
    tabHost.setup();
    TabHost.TabSpec
    tabSpec  = tabHost.newTabSpec("rano news");
    tabSpec.setContent(R.id.tabRanoNews);
    tabSpec.setIndicator("Rano News");
    tabHost.addTab(tabSpec);

    tabSpec  = tabHost.newTabSpec("adding form");
    tabSpec.setContent(R.id.tabAddingForm);
    tabSpec.setIndicator("Adding Form");
    tabHost.addTab(tabSpec);
    tabHost.getTabWidget().getChildTabViewAt(1).setEnabled(true);

只需将额外的额外参数传递给新 activity(在 Bundle 中)

    if (v.getId() == R.id.btnRanoNews2) {
        Intent i = new Intent(this, RanoNews.class);
        i.putExtra(RanoNews.TABHOST_ENABLED, false /* put your value here */);
        startActivity(i);
    }

并在新 activity 中处理它作为 onCreate() 方法的最后一行

    public static final String TABHOST_ENABLED = "tabhost_enabled";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        /* TabHost initialization */

        if (getIntent().getExtras() != null) {
            boolean isEnabled = getIntent().getExtras().getBoolean(TABHOST_ENABLED);
            tabHost.setEnabled(isEnabled);
        }
    }
public void OnClickRanoNews2(View v)
{
        if(v.getId()== R.id.btnRanoNews2)
        {
            Intent i = new Intent(this, RanoNews.class); 
            i.putExtra("msg", "0");
            startActivity(i);
        }
} 

第二个Activity

if(getIntent() != null){
      if(getIntent().getStringExtra("msg").equals("0")){
         tabHost.getTabWidget().getChildTabViewAt(1).setEnabled(true);
     }
}

这是我对第一个 Activity 代码所做的:

public void OnClickRanoNews(View v)
    {
        if (v.getId() == R.id.btnRanoNews)
        {
            Intent i = new Intent(this, RanoNews.class);
            i.putExtra("msg","0");
            startActivity(i);

        }

    }
    public void OnClickRanoNews2(View v)
    {
            if(v.getId()== R.id.btnRanoNews2)
            {
                Intent i = new Intent(this, RanoNews.class);
                i.putExtra("msg","1");
                startActivity(i);

            }
    }

第二个Activity代码:

tabHost = (TabHost)findViewById(R.id.tabHost);
        tabHost.setup();
        TabHost.TabSpec
        tabSpec  = tabHost.newTabSpec("rano news");
        tabSpec.setContent(R.id.tabRanoNews);
        tabSpec.setIndicator("Rano News");
        tabHost.addTab(tabSpec);

        tabSpec  = tabHost.newTabSpec("adding form");
        tabSpec.setContent(R.id.tabAddingForm);
        tabSpec.setIndicator("Adding Form");
        tabHost.addTab(tabSpec);

        if(getIntent()!=null)
        {
            if(getIntent().getStringExtra("msg").equals("0"))
                tabHost.getTabWidget().getChildTabViewAt(1).setEnabled(true);

            else if (getIntent().getStringExtra("msg").equals("1"))
                tabHost.getTabWidget().getChildTabViewAt(1).setVisibility(View.GONE);
        }