android - 我想以编程方式添加单选按钮并设置第一个项目被选中?
问题描述
这是我的代码,我从我的 Json 创建了一堆单选按钮。
for (int i = 0; i < level_array_list.size(); i++) {
RadioButton radioButton = new RadioButton(getActivity());
radioButton.setText(level_array_list.get(i).getLevelName());
radioButton.setId(Integer.parseInt(level_array_list.get(i).getLevel_id()));
radioGrp.addView(radioButton);
}
请帮忙。提前致谢!
解决方案
我想被选中从 json 数组加载的第一个项目?
您需要在(表示第一个单选按钮)radioButton.setChecked(true);
时使用内部循环i = 0
尝试这个
for (int i = 0; i < level_array_list.size(); i++) {
RadioButton radioButton = new RadioButton(getActivity());
radioButton.setText(level_array_list.get(i).getLevelName());
radioButton.setId(Integer.parseInt(level_array_list.get(i).getLevel_id()));
if(i==0){
radioButton.setChecked(true);
}
radioGrp.addView(radioButton);
}
推荐阅读
- html - 如何使用“位置:绝对”修复 div 底部的页脚?
- firebase - 将 Firebase 连接到使用 Expo 的 React Native 应用程序
- java - 我们可以将 avro 文件写入动态创建的 GCS 存储桶(基于tenantID)吗?
- excel - 如何在运行时使用字符串指定属性或方法。错误:未找到类型“范围”的公共成员“临时”
- graphql - 如何从 graphql 查询数据在 jsx 中创建循环?
- linux - Running find, grep and awk script
- linux - 将浮点数与整数进行比较以在 shell 编程中获得输出?
- security - 为什么拥有可缓存的 https 响应是错误的?
- python - 使用python在文本文件中搜索植物名称
- typescript - TypeScript 和模块选项设置为 es2015/es2020/esnext