在用taohost时候都是不同的Button来回切换的进入不同界面,但是,当点击第一个界面某一个Button或者ImageView是进第二个界面时候,并且需要显示tab时怎么办?
1首先 在MainActivity中:
public class MainActivity1 extends TabActivity implements
OnCheckedChangeListener {
private RadioGroup main_buttom_tabbar_group;
private TabHost tabHost;
private static final String HOME_ITEM = "home";
private static final String SHOP_ITEM = "shop";
private static final String USER_ITEM = "user";
private static final String MORE_ITEM = "more";
private Context context = this;
private RadioButton shop_item;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab_buttom);
initView();
initInfo();
initLis();
}
private void initView() {
tabHost = (TabHost) findViewById(android.R.id.tabhost);
main_buttom_tabbar_group = (RadioGroup) findViewById(R.id.main_buttom_tabbar_group);
shop_item = (RadioButton) findViewById(R.id.shop_item);
}
private void initInfo() {
Intent intent;
tabHost = this.getTabHost();
TabHost.TabSpec spec;
intent = new Intent(this, HomeActivity.class);
spec = tabHost.newTabSpec(HOME_ITEM).setIndicator(HOME_ITEM)
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent(this, EcardActivity.class);
spec = tabHost.newTabSpec(SHOP_ITEM).setIndicator(SHOP_ITEM)
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent(this, BlessInfoActivity.class);
spec = tabHost.newTabSpec(USER_ITEM).setIndicator(USER_ITEM)
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent(this, ViewActivity.class);
spec = tabHost.newTabSpec(MORE_ITEM).setIndicator(MORE_ITEM)
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
}
/****
* 这个方法就是解决出现的问题的
*/
public void toTwo() {
tabHost.setCurrentTab(1);
shop_item.setChecked(true);
}
private void initLis() {
main_buttom_tabbar_group.setOnCheckedChangeListener(this);
}
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.home_item:
tabHost.setCurrentTabByTag(HOME_ITEM);
break;
case R.id.shop_item:
tabHost.setCurrentTabByTag(SHOP_ITEM);
break;
case R.id.user_item:
tabHost.setCurrentTabByTag(USER_ITEM);
break;
case R.id.more_item:
tabHost.setCurrentTabByTag(MORE_ITEM);
break;
default:
break;
}
}
2然后 在第一个界面中:
@Override
public void onClick(View v) {
Intent intent = new Intent();
switch (v.getId()) {
case R.id.home_background:// 主页图片
((MainActivity1) getParent()).toTwo();
default:
break;
}
}