如何在从Activity调用时在Android中显示第二个选项卡

时间:2013-06-13 06:36:49

标签: android android-layout

这是我正在尝试的代码。如何从Tab2转到Sample?请帮我解决这个问题。

public class DemoTab extends TabActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search_and_login_tab);
        setTabs() ;
    }
    private void setTabs()
    {
        addTab("Tab1", R.drawable.tab_search, Tab1.class);
        addTab("Tab2", R.drawable.tab_home, Tab2.class);        
    }

    private void addTab(String labelId, int drawableId, Class<?> c)
    {
        TabHost tabHost = getTabHost();
        Intent intent = new Intent(this, c);
        TabHost.TabSpec spec = tabHost.newTabSpec("tab" + labelId); 

        View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, getTabWidget(), false);
        TextView title = (TextView) tabIndicator.findViewById(R.id.title);
        title.setText(labelId);
        ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
        icon.setImageResource(drawableId);

        spec.setIndicator(tabIndicator);
        spec.setContent(intent);
        tabHost.addTab(spec);
    }

}

Tab2的代码

public class Tab2 extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
            setContentView(R.layout.tab2);
    }


}

Tab1的代码

public class Tab1 extends Activity implements OnClickListener {
    Button check;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tab1);
        check= (Button) findViewById(R.id.check);
        check.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.check:
            //Do the Task Search Here
            Intent i = new Intent(getApplicationContext(), Sample.class);
            startActivity(i);
            break;

        }
    }
}

样本代码

public class Sample extends Activity implements OnClickListener {
    Button redirect;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search_result);
        redirect= (Button)findViewById(R.id.redirect);
        redirect.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.redirect:

这里我该怎么做才能看到Tab2,默认情况下,DemoTab的当前标签将是Tab1。

Intent i = new Intent(getApplicationContext(), DemoTab.class);
                startActivity(i);
            break;

        default:
            break;
        }
    }

}

3 个答案:

答案 0 :(得分:3)

试试这样......

当像这样调用tabactivity调用时

 Intent go=new Intent(this,TabActivity.class);
 Bundle b=new Bundle();
 b.putString("condition","tab2");
 go.putExtras(b);
 startActivity(go);

然后在tabAcitivity类

public class Demo extends TabActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Bundle b=getIntent().getExtras();
    String con=b.getString("condition");

    TabHost tab = getTabHost();
    TabSpec tab1 = (TabSpec) tab.newTabSpec("tb1");
    TabSpec tab2 = (TabSpec) tab.newTabSpec("tb2");
    tab1.setIndicator("Tab1").setContent(
            new Intent(this, Tab1.class));
    tab2.setIndicator("Tab2").setContent(
            new Intent(this, Tab2.class));

    tab.addTab(tab1);
    tab.addTab(tab2);
   if(con.equals("tab2")
     {
     tab.setCurrentTab(1);
     }
}
}

答案 1 :(得分:0)

setCurrentTab(index)打开默认显示的选项卡,由选项卡的索引位置指定。如果在创建TabActivity时设置tabHost.setCurrentTab(1);属性,它将根据需要显示第一个索引上的选项卡(0将其设置为第0个索引上的选项卡)。您可能还想检查以上一些问题:

android setting default tab in tab-activity

Start Android App in a specific tab

setCurrentTab of a tabHost

修改

Android TabHost setCurrentTab()

public class DemoTab extends TabActivity {
    //Declare this outside the elements
    TabHost tabHost;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //...
    }
    private void setTabs()
    {
        // ...
        addTab("Tab1", R.drawable.tab_search, Tab1.class);
        addTab("Tab2", R.drawable.tab_home, Tab2.class);  
        // Here you may try adding
        tabHost.setCurrentTab(1);    
    }

    private void addTab(String labelId, int drawableId, Class<?> c)
    {
        tabHost = getTabHost(); v//edit this line
        //....
    }

希望这有帮助

答案 2 :(得分:0)

我有两个类“One.Java”和“Two.Java”。我的主要活动是扩展'TabActivity'。在onCreate()方法中:

Intent one = new Intent(this, One.class);
Intent two = new Intent(this, Two.class);

TabHost t = (TabHost) findViewById(R.id.tabhost);
t.setup();

TabSpec tspecOne = t.newTabSpec("CLICK ONE").setIndicator("CLICK ONE")
            .setContent(one);
t.addTab(tspecOne);

TabSpec tspecTwo = t.newTabSpec("CLICK TWO")
            .setIndicator("CLICK TWO").setContent(two);
t.addTab(tspecTwo);