android tabbutton,android:TabHost结合RadioButton单选按钮实现Tab效果

public class TMainTabActivity extends TabActivity

implements

OnCheckedChangeListener {

RadioGroup radioGroup;

RadioButton[] radioButtons;

TabHost tabHost;

Intent weiboIntent;

Intent messageIntent;

Intent infoIntent;

Intent searchIntent;

Intent moreIntent;

@Override

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.mytab);

tabHost = getTabHost();

radioGroup = (RadioGroup) findViewById(R.id.radioGroup);

weiboIntent = new

Intent(MyWeiboActivity.INTENT_ACTION_FOR_MYHOME);

messageIntent = new Intent(TMainTabActivity.this,

MessageActivity.class);

infoIntent = new Intent(TMainTabActivity.this,

UserInfoActivity.class);

searchIntent = new Intent(TMainTabActivity.this,

SearchActivity.class);

moreIntent = new Intent(TMainTabActivity.this,

MoreActivity.class);

initButton();

setUpIntent();

radioButtons[tabHost.getCurrentTab()].setChecked(true);

}

private void setUpIntent() {

// TODO Auto-generated method stub

tabHost.clearAllTabs();

tabHost.addTab(tabHost.newTabSpec("tab01").setIndicator("tab")

.setContent(weiboIntent));

tabHost.addTab(tabHost.newTabSpec("tab02").setIndicator("tab")

.setContent(messageIntent));

tabHost.addTab(tabHost.newTabSpec("tab03").setIndicator("tab")

.setContent(infoIntent));

tabHost.addTab(tabHost.newTabSpec("tab04").setIndicator("tab")

.setContent(searchIntent));

tabHost.addTab(tabHost.newTabSpec("tab05").setIndicator("tab")

.setContent(moreIntent));

}

@Override

protected void onNewIntent(Intent intent) {

super.onNewIntent(intent);

tabHost.setCurrentTab(0);

radioButtons[tabHost.getCurrentTab()].setChecked(true);

}

protected void onResume() {

super.onResume();

}

private void initButton() {

// TODO Auto-generated method stub

radioButtons = new RadioButton[5];

for (int i = 0; i < 5; i++) {

radioButtons[i] = (RadioButton) radioGroup

.findViewWithTag("radio_button" + i);

radioButtons[i].setOnCheckedChangeListener(this);

}

}

@Override

public void onCheckedChanged(CompoundButton buttonView,

boolean isChecked) {

// TODO Auto-generated method stub

if (isChecked && tabHost !=

null) {

if (buttonView == radioButtons[0]) {

tabHost.setCurrentTabByTag("tab01");

} else if (buttonView == radioButtons[1]) {

tabHost.setCurrentTabByTag("tab02");

} else if (buttonView == radioButtons[2]) {

tabHost.setCurrentTabByTag("tab03");

} else if (buttonView == radioButtons[3]) {

tabHost.setCurrentTabByTag("tab04");

} else if (buttonView == radioButtons[4]) {

tabHost.setCurrentTabByTag("tab05");

}

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值