android 单选框如何动态渲染?-灵析社区

老詹啊老詹

`android` 的 `RadioGroup` 是通过 `RadioButton`的 `id` 来实现单选: 如果枚举是从服务端获取并且枚举随时可增加、减少,就需要动态渲染,通过编程方式动态往 `RadioGroup` 新增、减少 `RadioButton` 是一个方法,但感觉太繁琐了。用 `RecyclerView` 又会导致 `RadioButton` 的单选特性无效,需自行维护单选效果。 请问这种业务场景一般怎么实现?

阅读量:17

点赞量:0

问AI
七厦
RadioGroup radioGroup = findViewById(R.id.radioGroup); for (String option : optionsFromServer) { RadioButton radioButton = new RadioButton(this); radioButton.setText(option); radioButton.setId(View.generateViewId()); radioGroup.addView(radioButton); }