这是我正在尝试的代码。如何去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;
}
}
}