RadioGroup 是 Android 中用于表示一组单选按钮(Radio Button)的组件。它通常用于在一组选项中选择一个。以下是 RadioGroup 的一些常用属性和方法:
属性:
checked:表示当前选中的 RadioButton 的 ID。如果未选中任何 RadioButton,此属性值为 -1。checkedId:与 checked 相同,但返回的是被选中的 RadioButton 的 ID,而不是直接返回选中状态。count:当前 RadioGroup 中的 RadioButton 数量。id:RadioGroup 的唯一标识符。layoutDirection:设置 RadioGroup 中子项的布局方向。可以是 LinearLayout.VERTICAL 或 LinearLayout.HORIZONTAL。orientation:设置 RadioGroup 中子项的布局方向。可以是 Horizontal 或 Vertical。注意,此属性在 API 21 及以上版本中已被弃用,建议使用 layoutDirection 替代。onCheckedChanged:当 RadioGroup 中的 RadioButton 选中状态改变时触发的回调方法。方法:
check(int id):设置指定 ID 的 RadioButton 为选中状态。clearCheck():取消所有 RadioButton 的选中状态。isChecked(int id):检查指定 ID 的 RadioButton 是否被选中。setOnCheckedChangeListener(OnCheckedChangeListener listener):为 RadioGroup 设置一个 OnCheckedChangeListener,当选中状态改变时触发。这些属性和方法使得 RadioGroup 在 Android 开发中非常易于使用,可以实现单选按钮组的功能。