• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Toggle
2
3The **\<Toggle>** component provides a clickable element in the check box, button, or switch type.
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
11
12
13## Child Components
14
15This component can contain child components only when **ToggleType** is set to **Button**.
16
17
18## APIs
19
20Toggle(options: { type: ToggleType, isOn?: boolean })
21
22Since API version 9, this API is supported in ArkTS widgets.
23
24**Parameters**
25
26| Name| Type| Mandatory  | Description          |
27| ---- | ---------- | -----| -------------- |
28| type | [ToggleType](#toggletype) | Yes  | Type of the toggle.|
29| isOn | boolean    | No  | Whether the toggle is turned on. The value **true** means that the toggle is turned on, and **false** means the opposite.<br>Default value: **false**|
30
31
32## ToggleType
33
34Since API version 9, this API is supported in ArkTS widgets.
35
36| Name      | Description                |
37| -------- | ---------------- |
38| Checkbox | Check box type.<br>**NOTE**<br>The default value of the universal attribute [padding](ts-universal-attributes-size.md) is as follows:<br>{<br> top: 14 vp,<br> right: 6 vp,<br> bottom: 14 vp,<br> left: 6 vp<br> } |
39| Button   | Button type. The set string, if any, will be displayed inside the button.      |
40| Switch   | Switch type.<br>**NOTE**<br>The default value of the universal attribute [padding](ts-universal-attributes-size.md) is as follows:<br>{<br> top: 12 vp,<br> right: 12 vp,<br> bottom: 12 vp,<br> left: 12 vp<br> } |
41
42
43## Attributes
44
45| Name               | Parameter                          | Description                 |
46| ---------------- | --------------------------- | ---------------------- |
47| selectedColor    | [ResourceColor](ts-types.md#resourcecolor)  | Background color of the component when it is turned on.<br>Since API version 9, this API is supported in ArkTS widgets.|
48| switchPointColor | [ResourceColor](ts-types.md#resourcecolor)  | Color of the circular slider when the component is of the **Switch** type.<br>**NOTE**<br>This attribute is valid only when **type** is set to **ToggleType.Switch**.<br>Since API version 9, this API is supported in ArkTS widgets.|
49
50
51## Events
52
53| Name| Description|
54| -------- | -------- |
55| onChange(callback: (isOn: boolean) =&gt; void) | Triggered when the toggle status changes.<br>Since API version 9, this API is supported in ArkTS widgets.|
56
57
58## Example
59
60```ts
61// xxx.ets
62@Entry
63@Component
64struct ToggleExample {
65  build() {
66    Column({ space: 10 }) {
67      Text('type: Switch').fontSize(12).fontColor(0xcccccc).width('90%')
68      Flex({ justifyContent: FlexAlign.SpaceEvenly, alignItems: ItemAlign.Center }) {
69        Toggle({ type: ToggleType.Switch, isOn: false })
70          .selectedColor('#007DFF')
71          .switchPointColor('#FFFFFF')
72          .onChange((isOn: boolean) => {
73            console.info('Component status:' + isOn)
74          })
75
76        Toggle({ type: ToggleType.Switch, isOn: true })
77          .selectedColor('#007DFF')
78          .switchPointColor('#FFFFFF')
79          .onChange((isOn: boolean) => {
80            console.info('Component status:' + isOn)
81          })
82      }
83
84      Text('type: Checkbox').fontSize(12).fontColor(0xcccccc).width('90%')
85      Flex({ justifyContent: FlexAlign.SpaceEvenly, alignItems: ItemAlign.Center }) {
86        Toggle({ type: ToggleType.Checkbox, isOn: false })
87          .size({ width: 20, height: 20 })
88          .selectedColor('#007DFF')
89          .onChange((isOn: boolean) => {
90            console.info('Component status:' + isOn)
91          })
92
93        Toggle({ type: ToggleType.Checkbox, isOn: true })
94          .size({ width: 20, height: 20 })
95          .selectedColor('#007DFF')
96          .onChange((isOn: boolean) => {
97            console.info('Component status:' + isOn)
98          })
99      }
100
101      Text('type: Button').fontSize(12).fontColor(0xcccccc).width('90%')
102      Flex({ justifyContent: FlexAlign.SpaceEvenly, alignItems: ItemAlign.Center }) {
103        Toggle({ type: ToggleType.Button, isOn: false }) {
104          Text('status button').fontColor('#182431').fontSize(12)
105        }.width(106)
106        .selectedColor('rgba(0,125,255,0.20)')
107        .onChange((isOn: boolean) => {
108          console.info('Component status:' + isOn)
109        })
110
111        Toggle({ type: ToggleType.Button, isOn: true }) {
112          Text('status button').fontColor('#182431').fontSize(12)
113        }.width(106)
114        .selectedColor('rgba(0,125,255,0.20)')
115        .onChange((isOn: boolean) => {
116          console.info('Component status:' + isOn)
117        })
118      }
119    }.width('100%').padding(24)
120  }
121}
122```
123
124![toggle](figures/toggle.gif)
125