radioButtons = new RadioButton[rgMain.getChildCount()]; //遍历RadioGroup
for (int i = 0; i < radioButtons.length; i++) {
radioButtons[i] = (RadioButton) rgMain.getChildAt(i);
radioButtons[i].setTag(i); //设置tag
radioButtons[i].setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b)
//切换ViewPager页面
showFragment((int) compoundButton.getTag()); //获取tag
}
});
}setTag,getTage复用
最新推荐文章于 2021-12-13 09:49:19 发布
本文介绍了一种在Android应用中实现RadioGroup与ViewPager组件联动的方法。通过为每个RadioButton设置监听器并在选择状态改变时更新ViewPager的当前显示页面,实现了流畅的用户交互体验。
2万+

被折叠的 条评论
为什么被折叠?



