1e41f4b71Sopenharmony_ci# Toggle
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci
4e41f4b71Sopenharmony_ciThe **Toggle** component provides a clickable element in the check box, button, or switch type, typically used to switch between two states. For details, see [Toggle](../reference/apis-arkui/arkui-ts/ts-basic-components-toggle.md).
5e41f4b71Sopenharmony_ci
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ci## Creating a Toggle
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ciYou can create a toggle by calling the following API:
10e41f4b71Sopenharmony_ci
11e41f4b71Sopenharmony_ci```ts
12e41f4b71Sopenharmony_ciToggle(options: { type: ToggleType, isOn?: boolean })
13e41f4b71Sopenharmony_ci```
14e41f4b71Sopenharmony_ci
15e41f4b71Sopenharmony_ciIn this API, **ToggleType** indicates the toggle type, which can be **Button**, **Checkbox**, or **Switch**, and **isOn** specifies whether the toggle is turned on.
16e41f4b71Sopenharmony_ci
17e41f4b71Sopenharmony_ciSince API version 11, the default style of the **\<Checkbox>** component is changed from rounded square to circle.
18e41f4b71Sopenharmony_ci
19e41f4b71Sopenharmony_ciThe API can be called in either of the following ways:
20e41f4b71Sopenharmony_ci
21e41f4b71Sopenharmony_ci
22e41f4b71Sopenharmony_ci- Create a toggle that does not contain child components.
23e41f4b71Sopenharmony_ci  This can be achieved by calling the API with **ToggleType** set to **Checkbox** or **Switch**.
24e41f4b71Sopenharmony_ci  
25e41f4b71Sopenharmony_ci  
26e41f4b71Sopenharmony_ci    ```ts
27e41f4b71Sopenharmony_ci  Toggle({ type: ToggleType.Checkbox, isOn: false })
28e41f4b71Sopenharmony_ci  Toggle({ type: ToggleType.Checkbox, isOn: true })
29e41f4b71Sopenharmony_ci    ```
30e41f4b71Sopenharmony_ci
31e41f4b71Sopenharmony_ci  ![en-us_image_0000001562940485](figures/en-us_image_0000001562940485.png)
32e41f4b71Sopenharmony_ci  
33e41f4b71Sopenharmony_ci  ```ts
34e41f4b71Sopenharmony_ci  Toggle({ type: ToggleType.Switch, isOn: false })
35e41f4b71Sopenharmony_ci  Toggle({ type: ToggleType.Switch, isOn: true })
36e41f4b71Sopenharmony_ci  ```
37e41f4b71Sopenharmony_ci  
38e41f4b71Sopenharmony_ci    ![en-us_image_0000001511421228](figures/en-us_image_0000001511421228.png)
39e41f4b71Sopenharmony_ci  
40e41f4b71Sopenharmony_ci- Create a toggle that contains a child component.
41e41f4b71Sopenharmony_ci  When **ToggleType** is set to **Button**, only one child component is allowed. If the child component has text set, the text content is displayed on the button.
42e41f4b71Sopenharmony_ci
43e41f4b71Sopenharmony_ci  ```ts
44e41f4b71Sopenharmony_ci  Toggle({ type: ToggleType.Button, isOn: false }) {
45e41f4b71Sopenharmony_ci    Text('status button')
46e41f4b71Sopenharmony_ci      .fontColor('#182431')
47e41f4b71Sopenharmony_ci      .fontSize(12)
48e41f4b71Sopenharmony_ci  }.width(100)
49e41f4b71Sopenharmony_ci  Toggle({ type: ToggleType.Button, isOn: true }) {
50e41f4b71Sopenharmony_ci    Text('status button')
51e41f4b71Sopenharmony_ci      .fontColor('#182431')
52e41f4b71Sopenharmony_ci      .fontSize(12)
53e41f4b71Sopenharmony_ci  }.width(100)
54e41f4b71Sopenharmony_ci  ```
55e41f4b71Sopenharmony_ci
56e41f4b71Sopenharmony_ci    ![en-us_image_0000001511900404](figures/en-us_image_0000001511900404.png)
57e41f4b71Sopenharmony_ci
58e41f4b71Sopenharmony_ci
59e41f4b71Sopenharmony_ci## Setting Styles
60e41f4b71Sopenharmony_ci
61e41f4b71Sopenharmony_ci- Use the **selectedColor** attribute to set the background color of the toggle for when it is turned on.
62e41f4b71Sopenharmony_ci
63e41f4b71Sopenharmony_ci  ```ts
64e41f4b71Sopenharmony_ci  Toggle({ type: ToggleType.Button, isOn: true }) {
65e41f4b71Sopenharmony_ci    Text('status button')
66e41f4b71Sopenharmony_ci    .fontColor('#182431')
67e41f4b71Sopenharmony_ci    .fontSize(12)
68e41f4b71Sopenharmony_ci  }.width(100).selectedColor(Color.Pink)
69e41f4b71Sopenharmony_ci  Toggle({ type: ToggleType.Checkbox, isOn: true })
70e41f4b71Sopenharmony_ci    .selectedColor(Color.Pink)
71e41f4b71Sopenharmony_ci  Toggle({ type: ToggleType.Switch, isOn: true })
72e41f4b71Sopenharmony_ci    .selectedColor(Color.Pink)
73e41f4b71Sopenharmony_ci  ```
74e41f4b71Sopenharmony_ci
75e41f4b71Sopenharmony_ci  ![en-us_image_0000001563060657](figures/en-us_image_0000001563060657.png)
76e41f4b71Sopenharmony_ci
77e41f4b71Sopenharmony_ci- Use the **switchPointColor** attribute to set the color of the circular slider. This attribute is valid only when **type** of the toggle is set to **ToggleType.Switch**.
78e41f4b71Sopenharmony_ci
79e41f4b71Sopenharmony_ci  ```ts
80e41f4b71Sopenharmony_ci  Toggle({ type: ToggleType.Switch, isOn: false })
81e41f4b71Sopenharmony_ci    .switchPointColor(Color.Pink)
82e41f4b71Sopenharmony_ci  Toggle({ type: ToggleType.Switch, isOn: true })
83e41f4b71Sopenharmony_ci    .switchPointColor(Color.Pink)
84e41f4b71Sopenharmony_ci  ```
85e41f4b71Sopenharmony_ci
86e41f4b71Sopenharmony_ci  ![en-us_image_0000001511421232](figures/en-us_image_0000001511421232.png)
87e41f4b71Sopenharmony_ci
88e41f4b71Sopenharmony_ci
89e41f4b71Sopenharmony_ci## Adding Events
90e41f4b71Sopenharmony_ci
91e41f4b71Sopenharmony_ciThe **Toggle** component supports the [universal events](../reference/apis-arkui/arkui-ts/ts-universal-events-click.md). In addition, it can be bound to the **onChange** event so that it responds with custom behavior after being turned on or off.
92e41f4b71Sopenharmony_ci
93e41f4b71Sopenharmony_ci
94e41f4b71Sopenharmony_ci```ts
95e41f4b71Sopenharmony_ciToggle({ type: ToggleType.Switch, isOn: false })
96e41f4b71Sopenharmony_ci  .onChange((isOn: boolean) => {
97e41f4b71Sopenharmony_ci      if(isOn) {
98e41f4b71Sopenharmony_ci        // Operation
99e41f4b71Sopenharmony_ci      }
100e41f4b71Sopenharmony_ci  })
101e41f4b71Sopenharmony_ci```
102e41f4b71Sopenharmony_ci
103e41f4b71Sopenharmony_ci
104e41f4b71Sopenharmony_ci## Example Scenario
105e41f4b71Sopenharmony_ci
106e41f4b71Sopenharmony_ciIn this example, the **Toggle** component is used to enable or disable Bluetooth.
107e41f4b71Sopenharmony_ci
108e41f4b71Sopenharmony_ci```ts
109e41f4b71Sopenharmony_ci// xxx.ets
110e41f4b71Sopenharmony_ciimport { promptAction } from '@kit.ArkUI';
111e41f4b71Sopenharmony_ci@Entry
112e41f4b71Sopenharmony_ci@Component
113e41f4b71Sopenharmony_cistruct ToggleExample {
114e41f4b71Sopenharmony_ci  @State BOnSt:promptAction.ShowToastOptions = {'message': 'Bluetooth is on.'}
115e41f4b71Sopenharmony_ci  @State BOffSt:promptAction.ShowToastOptions = {'message': 'Bluetooth is off.'}
116e41f4b71Sopenharmony_ci  build() {
117e41f4b71Sopenharmony_ci    Column() {
118e41f4b71Sopenharmony_ci      Row() {
119e41f4b71Sopenharmony_ci        Text("Bluetooth Mode")
120e41f4b71Sopenharmony_ci          .height(50)
121e41f4b71Sopenharmony_ci          .fontSize(16)
122e41f4b71Sopenharmony_ci      }
123e41f4b71Sopenharmony_ci      Row() {
124e41f4b71Sopenharmony_ci        Text("Bluetooth")
125e41f4b71Sopenharmony_ci          .height(50)
126e41f4b71Sopenharmony_ci          .padding({left: 10})
127e41f4b71Sopenharmony_ci          .fontSize(16)
128e41f4b71Sopenharmony_ci          .textAlign(TextAlign.Start)
129e41f4b71Sopenharmony_ci          .backgroundColor(0xFFFFFF)
130e41f4b71Sopenharmony_ci        Toggle({ type: ToggleType.Switch })
131e41f4b71Sopenharmony_ci          .margin({left: 200, right: 10})
132e41f4b71Sopenharmony_ci          .onChange((isOn: boolean) => {
133e41f4b71Sopenharmony_ci            if(isOn) {
134e41f4b71Sopenharmony_ci              promptAction.showToast(this.BOnSt)
135e41f4b71Sopenharmony_ci            } else {
136e41f4b71Sopenharmony_ci              promptAction.showToast(this.BOffSt)
137e41f4b71Sopenharmony_ci            }
138e41f4b71Sopenharmony_ci          })
139e41f4b71Sopenharmony_ci      }
140e41f4b71Sopenharmony_ci      .backgroundColor(0xFFFFFF)
141e41f4b71Sopenharmony_ci    }
142e41f4b71Sopenharmony_ci    .padding(10)
143e41f4b71Sopenharmony_ci    .backgroundColor(0xDCDCDC)
144e41f4b71Sopenharmony_ci    .width('100%')
145e41f4b71Sopenharmony_ci    .height('100%')
146e41f4b71Sopenharmony_ci  }
147e41f4b71Sopenharmony_ci}
148e41f4b71Sopenharmony_ci```
149e41f4b71Sopenharmony_ci
150e41f4b71Sopenharmony_ci
151e41f4b71Sopenharmony_ci![en-us_image_0000001511740448](figures/en-us_image_0000001511740448.png)
152