ts-basic-components-radio.md 3.5 KB
Newer Older
E
add doc  
esterzhou 已提交
1 2 3 4
# Radio

The **\<Radio>** component allows users to select from a set of mutually exclusive options.

E
ester.zhou 已提交
5 6 7
>  **NOTE**
>
>  This component is supported since API version 8. Updates will be marked with a superscript to indicate their earliest API version.
E
add doc  
esterzhou 已提交
8 9 10 11


## Child Components

E
ester.zhou 已提交
12
Not supported
E
add doc  
esterzhou 已提交
13 14 15 16 17 18


## APIs

Radio(options: {value: string, group: string})

E
ester.zhou 已提交
19 20
Since API version 9, this API is supported in ArkTS widgets.

E
ester.zhou 已提交
21
**Parameters**
E
add doc  
esterzhou 已提交
22

E
ester.zhou 已提交
23 24 25 26
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| value | string | Yes| Value of the current radio button.|
| 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.|
E
add doc  
esterzhou 已提交
27 28 29

## Attributes

E
ester.zhou 已提交
30
In addition to the [universal attributes](ts-universal-attributes-size.md), the following attributes are supported.
E
add doc  
esterzhou 已提交
31

E
ester.zhou 已提交
32 33
| Name| Type| Description|
| -------- | -------- | -------- |
E
ester.zhou 已提交
34
| checked | boolean | Whether the radio button is selected.<br>Default value: **false**<br>Since API version 9, this API is supported in ArkTS widgets.|
E
ester.zhou 已提交
35
| radioStyle<sup>10+</sup> | [RadioStyle](#radiostyle) | Style of the radio button in selected or deselected state.<br>Since API version 10, this API is supported in ArkTS widgets.|
E
add doc  
esterzhou 已提交
36 37 38

## Events

E
ester.zhou 已提交
39 40 41
In addition to the [universal events](ts-universal-events-click.md), the following events are supported.

| Name| Description|
E
add doc  
esterzhou 已提交
42
| -------- | -------- |
E
ester.zhou 已提交
43
| onChange(callback: (isChecked: boolean) => void) | Triggered when the selected state of the radio button changes.<br> - If **isChecked** is **true**, it indicates that the radio button changes from unselected to selected.<br> - If **isChecked** is **false**, it indicates that the radio button changes from selected to unselected.<br>Since API version 9, this API is supported in ArkTS widgets.|
E
add doc  
esterzhou 已提交
44

E
ester.zhou 已提交
45 46 47 48 49 50 51
## RadioStyle

| Name                  | Type                                      | Mandatory| Default Value | Description                  |
| ---------------------- | ------------------------------------------ | ---- | ------- | ---------------------- |
| checkedBackgroundColor | [ResourceColor](ts-types.md#resourcecolor) | No  | #007DFF | Color of the background when the radio button is selected.    |
| uncheckedBorderColor   | [ResourceColor](ts-types.md#resourcecolor) | No  | #182431 | Color of the border when the radio button is deselected.    |
| indicatorColor         | [ResourceColor](ts-types.md#resourcecolor) | No  | #FFFFFF | Color of the indicator when the radio button is selected.|
E
add doc  
esterzhou 已提交
52 53 54

## Example

E
ester.zhou 已提交
55 56
```ts
// xxx.ets
E
add doc  
esterzhou 已提交
57 58 59 60 61 62 63 64 65 66
@Entry
@Component
struct RadioExample {
  build() {
    Flex({ direction: FlexDirection.Row, justifyContent: FlexAlign.Center, alignItems: ItemAlign.Center }) {
      Column() {
        Text('Radio1')
        Radio({ value: 'Radio1', group: 'radioGroup' }).checked(true)
          .height(50)
          .width(50)
E
ester.zhou 已提交
67 68
          .onChange((isChecked: boolean) => {
            console.log('Radio1 status is ' + isChecked)
E
add doc  
esterzhou 已提交
69 70 71 72 73 74 75
          })
      }
      Column() {
        Text('Radio2')
        Radio({ value: 'Radio2', group: 'radioGroup' }).checked(false)
          .height(50)
          .width(50)
E
ester.zhou 已提交
76 77
          .onChange((isChecked: boolean) => {
            console.log('Radio2 status is ' + isChecked)
E
add doc  
esterzhou 已提交
78 79 80 81 82 83 84
          })
      }
      Column() {
        Text('Radio3')
        Radio({ value: 'Radio3', group: 'radioGroup' }).checked(false)
          .height(50)
          .width(50)
E
ester.zhou 已提交
85 86
          .onChange((isChecked: boolean) => {
            console.log('Radio3 status is ' + isChecked)
E
add doc  
esterzhou 已提交
87 88 89 90 91 92 93
          })
      }
    }.padding({ top: 30 })
  }
}
```
![](figures/radio.gif)