解决tabhost底部tab问题

在用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;
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值