1# Radio 2 3The **Radio** component allows users to select from a set of mutually exclusive options. 4 5> **NOTE** 6> 7> This component is supported since API version 8. Updates will be marked with a superscript to indicate their earliest API version. 8 9 10## Child Components 11 12Not supported 13 14 15## APIs 16 17Radio(options: RadioOptions) 18 19Creates a radio button. 20 21**Widget capability**: This API can be used in ArkTS widgets since API version 9. 22 23**Atomic service API**: This API can be used in atomic services since API version 11. 24 25**System capability**: SystemCapability.ArkUI.ArkUI.Full 26 27**Parameters** 28 29| Name | Type | Mandatory | Description | 30| ------- | ------------------------------------- | ---- | ------------------ | 31| options | [RadioOptions](#radiooptions) | Yes | Parameters of the radio button. | 32 33## RadioOptions 34 35| Name | Type | Mandatory | Description | 36| -------- | -------- | -------- | -------- | 37| value | string | Yes | Current value of the radio button.<br>**Atomic service API**: This API can be used in atomic services since API version 11. | 38| group | string | Yes | Name of the group to which the radio button belongs. Only one radio button in a given group can be selected at a time.<br>**Atomic service API**: This API can be used in atomic services since API version 11. | 39| indicatorType<sup>12+</sup> | [RadioIndicatorType](#radioindicatortype12) | No | Indicator type of the radio button. If no value is specified, the value of **RadioIndicatorType.TICK** is used.<br>**Atomic service API**: This API can be used in atomic services since API version 12.| 40| indicatorBuilder<sup>12+</sup> | [CustomBuilder](ts-types.md#custombuilder8) | No | Custom component to indicate that the radio button is selected. This custom component is center aligned with the radio button. If this parameter is set to **undefined**, the value of **RadioIndicatorType.TICK** is used as the indicator type.<br>**Atomic service API**: This API can be used in atomic services since API version 12.| 41 42## RadioIndicatorType<sup>12+</sup> 43 44**Atomic service API**: This API can be used in atomic services since API version 12. 45 46| Name | Description | 47| --------------- | -------------------------------- | 48| TICK | Default tick icon. | 49| DOT | Default dot icon. | 50| CUSTOM | Custom component.| 51 52## Attributes 53 54In addition to the [universal attributes](ts-universal-attributes-size.md), the following attributes are supported. 55 56### checked 57 58checked(value: boolean) 59 60Sets whether the radio button is selected. 61 62Since API version 10, this attribute supports two-way binding through [$$](../../../quick-start/arkts-two-way-sync.md). 63 64**Widget capability**: This API can be used in ArkTS widgets since API version 9. 65 66**Atomic service API**: This API can be used in atomic services since API version 11. 67 68**System capability**: SystemCapability.ArkUI.ArkUI.Full 69 70**Parameters** 71 72| Name | Type | Mandatory | Description | 73| ------ | ------- | ---- | ------------------------------------ | 74| value | boolean | Yes | Whether the radio button is selected.<br>Default value: **false** | 75 76### radioStyle<sup>10+</sup> 77 78radioStyle(value?: RadioStyle) 79 80Sets the style of the radio button in selected or deselected state. 81 82Since API version 10, this API is supported in ArkTS widgets. 83 84**Atomic service API**: This API can be used in atomic services since API version 11. 85 86**System capability**: SystemCapability.ArkUI.ArkUI.Full 87 88**Parameters** 89 90| Name | Type | Mandatory | Description | 91| ------ | ----------------------------------- | ---- | ---------------------------------- | 92| value | [RadioStyle](#radiostyle10) | No | Style of the radio button in selected or deselected state. | 93 94## contentModifier<sup>12+</sup> 95 96contentModifier(modifier: ContentModifier\<RadioConfiguration>) 97 98Creates a content modifier. 99 100**Atomic service API**: This API can be used in atomic services since API version 12. 101 102**System capability**: SystemCapability.ArkUI.ArkUI.Full 103 104**Parameters** 105 106| Name | Type | Mandatory | Description | 107| ------ | --------------------------------------------- | ---- | ------------------------------------------------ | 108| modifier | [ContentModifier\<RadioConfiguration>](#radioconfiguration12) | Yes | Content modifier to apply to the current component.<br>**modifier**: content modifier. You need a custom class to implement the **ContentModifier** API. | 109 110## Events 111 112In addition to the [universal events](ts-universal-events-click.md), the following events are supported. 113 114### onChange 115 116onChange(callback: (isChecked: boolean) => void) 117 118Triggered when the selected state of the radio button changes. 119 120**Widget capability**: This API can be used in ArkTS widgets since API version 9. 121 122**Atomic service API**: This API can be used in atomic services since API version 11. 123 124**System capability**: SystemCapability.ArkUI.ArkUI.Full 125 126**Parameters** 127 128| Name | Type | Mandatory | Description | 129| --------- | ------- | ---- | ------------------------------------------------------------ | 130| isChecked | boolean | Yes | Selected state of the radio button.<br>The value **true** means that the radio button changes from unselected to selected, and **false** means that the radio button changes from selected to unselected. | 131 132## RadioStyle<sup>10+</sup> 133 134**Atomic service API**: This API can be used in atomic services since API version 11. 135 136| Name | Type | Mandatory | Default Value | Description | 137| ---------------------- | ------------------------------------------ | ---- | ------- | ---------------------- | 138| checkedBackgroundColor | [ResourceColor](ts-types.md#resourcecolor) | No | #007DFF | Color of the background when the radio button is selected. | 139| uncheckedBorderColor | [ResourceColor](ts-types.md#resourcecolor) | No | #182431 | Color of the border when the radio button is deselected. | 140| indicatorColor | [ResourceColor](ts-types.md#resourcecolor) | No | #FFFFFF | Color of the indicator when the radio button is selected. Since API version 12, this parameter takes effect only when **indicatorType** is set to **RadioIndicatorType.TICK** or **RadioIndicatorType.DOT**. | 141 142## RadioConfiguration<sup>12+</sup> 143 144You need a custom class to implement the **ContentModifier** API. 145 146**Atomic service API**: This API can be used in atomic services since API version 12. 147 148| Name | Type | Default Value | Description | 149| ------ | ------ | ------ |-------------------------------- | 150| value | string | - |Current value of the radio button. | 151| checked | boolean| false | Whether the radio button is selected.| 152| triggerChange |Callback\<boolean>| - |Changes the selected state of the radio button. | 153 154 155## Example 156### Example 1 157This example shows how to set the color of the background when the radio button is selected. 158```ts 159// xxx.ets 160@Entry 161@Component 162struct RadioExample { 163 build() { 164 Flex({ direction: FlexDirection.Row, justifyContent: FlexAlign.Center, alignItems: ItemAlign.Center }) { 165 Column() { 166 Text('Radio1') 167 Radio({ value: 'Radio1', group: 'radioGroup' }).checked(true) 168 .radioStyle({ 169 checkedBackgroundColor: Color.Pink 170 }) 171 .height(50) 172 .width(50) 173 .onChange((isChecked: boolean) => { 174 console.log('Radio1 status is ' + isChecked) 175 }) 176 } 177 Column() { 178 Text('Radio2') 179 Radio({ value: 'Radio2', group: 'radioGroup' }).checked(false) 180 .radioStyle({ 181 checkedBackgroundColor: Color.Pink 182 }) 183 .height(50) 184 .width(50) 185 .onChange((isChecked: boolean) => { 186 console.log('Radio2 status is ' + isChecked) 187 }) 188 } 189 Column() { 190 Text('Radio3') 191 Radio({ value: 'Radio3', group: 'radioGroup' }).checked(false) 192 .radioStyle({ 193 checkedBackgroundColor: Color.Pink 194 }) 195 .height(50) 196 .width(50) 197 .onChange((isChecked: boolean) => { 198 console.log('Radio3 status is ' + isChecked) 199 }) 200 } 201 }.padding({ top: 30 }) 202 } 203} 204``` 205 206### Example 2 207This example shows how to set the indicator of the radio button to an image. 208```ts 209// xxx.ets 210@Entry 211@Component 212struct RadioExample { 213 @Builder 214 indicatorBuilder() { 215 Image($r("app.media.star")) 216 } 217 build() { 218 Flex({ direction: FlexDirection.Row, justifyContent: FlexAlign.Center, alignItems: ItemAlign.Center }) { 219 Column() { 220 Text('Radio1') 221 Radio({ value: 'Radio1', group: 'radioGroup', 222 indicatorType:RadioIndicatorType.TICK 223 }).checked(true) 224 .height(50) 225 .width(80) 226 .onChange((isChecked: boolean) => { 227 console.log('Radio1 status is ' + isChecked) 228 }) 229 } 230 Column() { 231 Text('Radio2') 232 Radio({ value: 'Radio2', group: 'radioGroup', 233 indicatorType:RadioIndicatorType.DOT 234 }).checked(false) 235 .height(50) 236 .width(80) 237 .onChange((isChecked: boolean) => { 238 console.log('Radio2 status is ' + isChecked) 239 }) 240 } 241 Column() { 242 Text('Radio3') 243 Radio({ value: 'Radio3', group: 'radioGroup', 244 indicatorType:RadioIndicatorType.CUSTOM, 245 indicatorBuilder:()=>{this.indicatorBuilder()} 246 }).checked(false) 247 .height(50) 248 .width(80) 249 .onChange((isChecked: boolean) => { 250 console.log('Radio3 status is ' + isChecked) 251 }) 252 } 253 }.padding({ top: 30 }) 254 } 255} 256``` 257 258### Example 3 259This example shows how to set the style of the radio button in selected or deselected state. 260```ts 261class MyRadioStyle implements ContentModifier<RadioConfiguration> { 262 type: number = 0 263 selectedColor:Color = Color.Black 264 265 constructor(numberType: number, colorType:Color) { 266 this.type = numberType 267 this.selectedColor = colorType 268 } 269 270 applyContent() : WrappedBuilder<[RadioConfiguration]> 271 { 272 return wrapBuilder(buildRadio) 273 } 274} 275 276@Builder function buildRadio(config: RadioConfiguration) { 277 Row({ space:30 }) { 278 Circle({ width: 50, height: 50 }) 279 .stroke(Color.Black) 280 .fill(config.checked ? (config.contentModifier as MyRadioStyle).selectedColor : Color.White) 281 Button(config.checked ? "off" : "on") 282 .width(100) 283 .type(config.checked ? (config.contentModifier as MyRadioStyle).type : ButtonType.Normal) 284 .backgroundColor(0xAABBCC) 285 .onClick(()=>{ 286 if (config.checked) { 287 config.triggerChange(false) 288 } else { 289 config.triggerChange(true) 290 } 291 }) 292 } 293} 294 295@Entry 296@Component 297struct refreshExample { 298 build() { 299 Column({ space: 50 }) { 300 Row() { 301 Radio({ value: 'Radio1', group: 'radioGroup' }) 302 .contentModifier(new MyRadioStyle(1, Color.Red)) 303 .checked(false) 304 .width(300) 305 .height(100) 306 } 307 Row() { 308 Radio({ value: 'Radio2', group: 'radioGroup' }) 309 .checked(true) 310 .width(300) 311 .height(60) 312 .contentModifier(new MyRadioStyle(2, Color.Red)) 313 } 314 } 315 } 316} 317``` 318 319