RadioGroup动态添加RadioButton_并且获得事件
2014-06-16 13:28:46  By: shinyuu

由于有许多的RadioButton是动态的,不是固定的一些,所以需要在代码中,动态的添加到RadioGroup中,下面是我的实现方法。

1、添加RadioButton到RadioGroup中


RadioGroup group;  
for(int i=0; i<10; i++)  
{  
    RadioButton tempButton = new RadioButton(this);  
    tempButton.setBackgroundResource(R.drawable.xxx);   // 设置RadioButton的背景图片  
    tempButton.setButtonDrawable(R.drawable.xxx);           // 设置按钮的样式  
    tempButton.setPadding(80, 0, 0, 0);                 // 设置文字距离按钮四周的距离   
    tempButton.setText("按钮 " + i);  
    group.addView(tempButton, LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);  
}  

2、为RadioGroup添加事件处理,可以得到当前选择的RadioButton

group.setOnCheckedChangeListener(new OnCheckedChangeListener() {  
      
    @Override  
    public void onCheckedChanged(RadioGroup group, int checkedId) {  
        // 通过RadioGroup的findViewById方法,找到ID为checkedID的RadioButton
        RadioButton tempButton = (RadioButton)findViewById(checkedId); 
        // 以下就可以对这个RadioButton进行处理了  
    }  
});  

若资源对你有帮助、浏览后有很大收获、不妨小额打赏我一下、你的鼓励是维持我不断写博客最大动力

想获取DD博客最新代码、你可以扫描下方的二维码、关注DD博客微信公众号(ddblogs)

或者你也可以关注我的新浪微博、了解DD博客的最新动态:DD博客官方微博(dwtedx的微博)

如对资源有任何疑问或觉得仍然有很大的改善空间、可以对该博文进行评论、希望不吝赐教

为保证及时回复、可以使用博客留言板给我留言: DD博客留言板(dwtedx的留言板)

感谢你的访问、祝你生活愉快、工作顺心、欢迎常来逛逛


快速评论


技术评论

  • 该技术还没有评论、赶快抢沙发吧...
DD记账
top
+