ts-basic-components-radio.md 2.6 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
add doc  
esterzhou 已提交
35 36 37

## Events

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

| Name| Description|
E
add doc  
esterzhou 已提交
41
| -------- | -------- |
E
ester.zhou 已提交
42
| 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.<br>Since API version 9, this API is supported in ArkTS widgets.|
E
add doc  
esterzhou 已提交
43 44 45 46


## Example

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