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中 ,这样的话 , 多出的部分可以滚动查看了。

添加RadioButton到RadioGroup中

  1. RadioGroup group;
  2. for(int i=0; i<10; i++)
  3. {
  4. RadioButton tempButton = new RadioButton(this);
  5. tempButton.setBackgroundResource(R.drawable.xxx);   // 设置RadioButton的背景图片
  6. tempButton.setButtonDrawable(R.drawable.xxx);           // 设置按钮的样式
  7. tempButton.setPadding(80, 0, 0, 0);                 // 设置文字距离按钮四周的距离
  8. tempButton.setText("按钮 " + i);
  9. group.addView(tempButton, LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
  10. }
  1. 2、为RadioGroup添加事件处理,可以得到当前选择的RadioButton
  1. group.setOnCheckedChangeListener(new OnCheckedChangeListener() {
  2. @Override
  3. public void onCheckedChanged(RadioGroup group, int checkedId) {
  4. // TODO Auto-generated method stub
  5. RadioButton tempButton = (RadioButton)findViewById(checkedId); // 通过RadioGroup的findViewById方法,找到ID为checkedID的RadioButton
  6. // 以下就可以对这个RadioButton进行处理了
  7. }
  8. });

最新文章

  1. 前nginx后Apache+Node反向代理
  2. C# Memcache分布式缓存简单入门
  3. Discuz X3核心文件解析
  4. 细雨学习笔记:JMeter 的主要测试组件总结
  5. 【Linux远程管理】Telnet远程连接管理
  6. POJ 2502 Subway(迪杰斯特拉)
  7. jquery $.each 和for 怎么跳出循环
  8. 搭建自己的CA服务 - OpenSSL CA 实战
  9. spring2——IOC之Bean的装配
  10. readLine()的注意点
  11. BizTalk Schedule Adapter的使用
  12. 【一些容易忘记的node的npm命令】【收集】
  13. Salesforce服务云简介
  14. C#自定义无边框MessageBox窗体
  15. N!
  16. 昂达 v891 连接上adb 调试
  17. Android中的缩略图加载-不浪费一点多余的内存
  18. leetCode题解之判断一个句子中的字符和数字是否构成回文
  19. springboot快速入门(四)——数据库操作
  20. JDK1.8最新特性--Lambda表达式(重点)

热门文章

  1. 新数据的GT列表
  2. Linux文件操作函数
  3. JavaScript -- 语法和数据类型
  4. Java形式参数和返回值的问题
  5. 请大神看看10.10Beta1的AppleRTC怎么破?原版会导致BIOS重置!
  6. 1008: ASCII码
  7. Mysql 5.7在Linux上部署及远程访问
  8. docker系列之file基本操作
  9. pandas.DataFrame——pd数据框的简单认识、存csv文件
  10. 入门人工智能的首选语言为什么会是Python?