ts-basic-components-checkbox.md 1.9 KB
Newer Older
E
esterzhou 已提交
1 2 3 4
# Checkbox

The **\<Checkbox>** component is used to enable or disable an option.

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

## Child Components

E
ester.zhou 已提交
11
Not supported
E
esterzhou 已提交
12 13 14

## APIs

E
ester.zhou 已提交
15
Checkbox(options?: {name?: string,  group?: string })
E
esterzhou 已提交
16

E
ester.zhou 已提交
17
**Parameters**
E
esterzhou 已提交
18

E
ester.zhou 已提交
19 20 21 22
| Name | Type| Mandatory | Description|
| --------| --------| ------ | -------- |
| name    | string | No| Name of the check box.|
| group   | string | No| Group name of the check box.|
E
esterzhou 已提交
23 24 25

## Attributes

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

E
ester.zhou 已提交
28 29 30 31

| Name         | Type| Description|
| ------------- | ------- | -------- |
| select        | boolean | Whether the check box is selected.<br>Default value: **false**|
E
ester.zhou 已提交
32
| selectedColor | [ResourceColor](ts-types.md#resourcecolor) | Color of the check box when it is selected.|
E
esterzhou 已提交
33 34 35

## Events

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

E
esterzhou 已提交
38
| Name     | Description|
E
esterzhou 已提交
39
| ----------| -------- |
E
esterzhou 已提交
40 41
|onChange(callback: (value: boolean) => void) | Triggered when the selection status of the check box changes.<br>- The value **true** means that the check box is selected.<br>- The value **false** means that the check box is not selected.|

E
esterzhou 已提交
42 43
## Example

E
ester.zhou 已提交
44 45
```ts
// xxx.ets
E
esterzhou 已提交
46
@Entry
E
esterzhou 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
@Component
struct CheckboxExample {

  build() {
    Row() {
      Checkbox({name: 'checkbox1',  group: 'checkboxGroup'})
        .select(true)
        .selectedColor(0xed6f21)
        .onChange((value: boolean) => {
          console.info('Checkbox1 change is'+ value)
        })
      Checkbox({name: 'checkbox2',  group: 'checkboxGroup'})
        .select(false)
        .selectedColor(0x39a2db)
        .onChange((value: boolean) => {
          console.info('Checkbox2 change is'+ value)
        })
    }
  }
}
```


![](figures/checkbox.gif)