android RadioGroup与TabHost组合使用达到菜单效果
16lz
2021-01-23
public class MainActivity extends TabActivity implements OnCheckedChangeListener {
private RadioGroup mainTab;
private TabHost tabhost;
private Intent iHome;//首页
private Intent iCart;//购物车
private Intent iOpt;//设置
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);//无窗体显示
setContentView(R.layout.main);
initTabHost();
}
/**
* 初始化Tab数据
*/
private void initTabHost(){
mainTab=(RadioGroup)findViewById(R.id.main_tab);
mainTab.setOnCheckedChangeListener(this);
tabhost = getTabHost();
iHome = new Intent(this, HomeActivity.class);
tabhost.addTab(tabhost.newTabSpec("iHome")
.setIndicator(getResources().getString(R.string.main_home), getResources().getDrawable(R.drawable.icon_1_n))
.setContent(iHome));
iCart = new Intent(this, CartActivity.class);
tabhost.addTab(tabhost.newTabSpec("iCart")
.setIndicator(getResources().getString(R.string.main_xy), getResources().getDrawable(R.drawable.icon_2_n))
.setContent(iCart));
iOpt = new Intent(this, OptionActivity.class);
tabhost.addTab(tabhost.newTabSpec("iOpt")
.setIndicator(getResources().getString(R.string.main_opt), getResources().getDrawable(R.drawable.icon_3_n))
.setContent(iOpt));
}
@Override
public void onCheckedChanged(RadioGroup group, int checkedId){//根据选择ID来显示相应的Tab
switch(checkedId){
case R.id.radio_button0:
this.tabhost.setCurrentTabByTag("iHome");
break;
case R.id.radio_button1:
this.tabhost.setCurrentTabByTag("iCart");
break;
case R.id.radio_button2:
this.tabhost.setCurrentTabByTag("iOpt");
break;
}
}
}
private RadioGroup mainTab;
private TabHost tabhost;
private Intent iHome;//首页
private Intent iCart;//购物车
private Intent iOpt;//设置
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);//无窗体显示
setContentView(R.layout.main);
initTabHost();
}
/**
* 初始化Tab数据
*/
private void initTabHost(){
mainTab=(RadioGroup)findViewById(R.id.main_tab);
mainTab.setOnCheckedChangeListener(this);
tabhost = getTabHost();
iHome = new Intent(this, HomeActivity.class);
tabhost.addTab(tabhost.newTabSpec("iHome")
.setIndicator(getResources().getString(R.string.main_home), getResources().getDrawable(R.drawable.icon_1_n))
.setContent(iHome));
iCart = new Intent(this, CartActivity.class);
tabhost.addTab(tabhost.newTabSpec("iCart")
.setIndicator(getResources().getString(R.string.main_xy), getResources().getDrawable(R.drawable.icon_2_n))
.setContent(iCart));
iOpt = new Intent(this, OptionActivity.class);
tabhost.addTab(tabhost.newTabSpec("iOpt")
.setIndicator(getResources().getString(R.string.main_opt), getResources().getDrawable(R.drawable.icon_3_n))
.setContent(iOpt));
}
@Override
public void onCheckedChanged(RadioGroup group, int checkedId){//根据选择ID来显示相应的Tab
switch(checkedId){
case R.id.radio_button0:
this.tabhost.setCurrentTabByTag("iHome");
break;
case R.id.radio_button1:
this.tabhost.setCurrentTabByTag("iCart");
break;
case R.id.radio_button2:
this.tabhost.setCurrentTabByTag("iOpt");
break;
}
}
}
更多相关文章
- Android任意窗口添加固定/浮动窗体:音乐播放器底部/顶部常驻播放
- Android 解决Google搜索首页的文本框清空按钮问题
- Android 初始化ini.rc中启动zygote服务
- Android获取窗体信息的Util方法
- 20150602_Andriod 向窗体传递参数
- Android getWindow().setFlags方法 设置窗体全屏点亮背景模糊 &&
- android 程序初始化界面.. .
- android设计 实现窗体启动时,显示一个水平进度条,当进行完成后,隐藏
- android中的类加载和静态成员变量的初始化