android - 如何使用 kotlin android 扩展从 radiogroup 中检索单选按钮
问题描述
我正在以编程方式从片段中的单选组中添加和删除单选按钮。onCheckedChangeListener 提供对 radiogroup 的引用和选中的单选按钮的整数 id。我可以使用 findViewById 获得对单选按钮的引用,但我可以使用 kotlin android 扩展来做到这一点吗?
class MyFragment: Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
radio_group.setOnCheckedChangeListener({ radioGroup: RadioGroup, i: Int ->
// This may give index out of bounds exception
someFunction((radioGroup[i-1] as RadioButton).text as String)
// This works but I don't want to use findViewById
val rb: RadioButton = radioGroup.findViewById(i) as RadioButton
onRadioButtonSelect(rb.text as String)
})
}
}
解决方案
Kotlin 扩展是编译时的。但是,您希望在运行时找到 id。因此,您将无法使用带有动态添加单选按钮的 kotlin 扩展
推荐阅读
- java - 是否可以在 java 中自动化 ffmpeg-normalize?
- haskell - 计算小写和大写,然后将其作为元组返回
- java - BorderLayout 为较小的 JLabel 保留空间
- intellij-idea - JetBrains IntelliJ IDEA 删除歧管插件
- go - 将 golang 应用程序部署到 Heroku 时运行迁移
- c++ - How would I use type as a "variable"?
- javascript - AJAX/PHP - 以纯 JavaScript 分段上传大文件
- ios - UIView 和屏幕旋转上的对角渐变背景
- python - 展开嵌套的字符串列表并保存为 .txt
- visual-c++ - msbuild-bin/cl.exe 不知道 std::apply 是什么?