1

这是我正在尝试的代码。如何去Tab2Sample?请帮我解决这个问题。

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

}
4

3 回答 3

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);
     }
}
}
于 2013-06-13T06:58:50.860 回答
0

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

android在选项卡活动中设置默认选项卡

在特定选项卡中启动 Android 应用

tabHost 的 setCurrentTab

编辑

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

希望这可以帮助

于 2013-06-13T06:48:21.570 回答
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);
于 2013-06-13T06:52:34.703 回答