ts-basic-components-radio.md 2.4 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 12 13 14 15 16 17 18


## Child Components

None


## APIs

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

E
ester.zhou 已提交
19
**Parameters**
E
add doc  
esterzhou 已提交
20

E
ester.zhou 已提交
21 22 23 24
| 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 已提交
25 26 27

## Attributes

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

E
ester.zhou 已提交
30 31 32
| Name| Type| Description|
| -------- | -------- | -------- |
| checked | boolean | Whether the radio button is selected.<br>Default value: **false**|
E
add doc  
esterzhou 已提交
33 34 35

## Events

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

| Name| Description|
E
add doc  
esterzhou 已提交
39
| -------- | -------- |
E
ester.zhou 已提交
40
| onChange(callback: (isChecked: boolean) => void) | Triggered when the selected state of the radio button changes.<br> - If **isChecked** is **true**, the radio button is selected.<br> - If **isChecked** is **false**, the radio button is not selected.|
E
add doc  
esterzhou 已提交
41 42 43 44


## Example

E
ester.zhou 已提交
45 46
```ts
// xxx.ets
E
add doc  
esterzhou 已提交
47 48 49 50 51 52 53 54 55 56
@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 已提交
57 58
          .onChange((isChecked: boolean) => {
            console.log('Radio1 status is ' + isChecked)
E
add doc  
esterzhou 已提交
59 60 61 62 63 64 65
          })
      }
      Column() {
        Text('Radio2')
        Radio({ value: 'Radio2', group: 'radioGroup' }).checked(false)
          .height(50)
          .width(50)
E
ester.zhou 已提交
66 67
          .onChange((isChecked: boolean) => {
            console.log('Radio2 status is ' + isChecked)
E
add doc  
esterzhou 已提交
68 69 70 71 72 73 74
          })
      }
      Column() {
        Text('Radio3')
        Radio({ value: 'Radio3', group: 'radioGroup' }).checked(false)
          .height(50)
          .width(50)
E
ester.zhou 已提交
75 76
          .onChange((isChecked: boolean) => {
            console.log('Radio3 status is ' + isChecked)
E
add doc  
esterzhou 已提交
77 78 79 80 81 82 83
          })
      }
    }.padding({ top: 30 })
  }
}
```
![](figures/radio.gif)