动态添加radiogroup
2024-08-24 14:33:23
private LinearLayout layout; //布局 , 可以在xml布局中获得
private RadioGroup group ; //点选按钮组
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
layout = new LinearLayout(this); //实例化布局对象
group = new RadioGroup(this); //实例化单选按钮组
//添加单选按钮
for(int i = 0 ; i < 5 ; i++){
RadioButton radio = new RadioButton(this);
radio.setText("radio" + i);
group.addView(radio);
}
//将单选按钮组添加到布局中
layout.addView(group);
this.setContentView(layout);
}
可以把单选按钮组放在 ScrollView中 ,这样的话 , 多出的部分可以滚动查看了。
- 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) {
- // TODO Auto-generated method stub
- RadioButton tempButton = (RadioButton)findViewById(checkedId); // 通过RadioGroup的findViewById方法,找到ID为checkedID的RadioButton
- // 以下就可以对这个RadioButton进行处理了
- }
- });
最新文章
- 前nginx后Apache+Node反向代理
- C# Memcache分布式缓存简单入门
- Discuz X3核心文件解析
- 细雨学习笔记:JMeter 的主要测试组件总结
- 【Linux远程管理】Telnet远程连接管理
- POJ 2502 Subway(迪杰斯特拉)
- jquery $.each 和for 怎么跳出循环
- 搭建自己的CA服务 - OpenSSL CA 实战
- spring2——IOC之Bean的装配
- readLine()的注意点
- BizTalk Schedule Adapter的使用
- 【一些容易忘记的node的npm命令】【收集】
- Salesforce服务云简介
- C#自定义无边框MessageBox窗体
- N!
- 昂达 v891 连接上adb 调试
- Android中的缩略图加载-不浪费一点多余的内存
- leetCode题解之判断一个句子中的字符和数字是否构成回文
- springboot快速入门(四)——数据库操作
- JDK1.8最新特性--Lambda表达式(重点)