1e41f4b71Sopenharmony_ci# switch 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci> **NOTE** 4e41f4b71Sopenharmony_ci> 5e41f4b71Sopenharmony_ci> This component is supported since API version 4. Updates will be marked with a superscript to indicate their earliest API version. 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ciThe **\<switch>** component is used to enable or disable a function. 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci## Required Permissions 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ciNone 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci 14e41f4b71Sopenharmony_ci## Child Components 15e41f4b71Sopenharmony_ci 16e41f4b71Sopenharmony_ciNot supported 17e41f4b71Sopenharmony_ci 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ci## Attributes 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ciIn addition to the [universal attributes](js-components-common-attributes.md), the following attributes are supported. 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci| Name | Type | Default Value | Mandatory | Description | 24e41f4b71Sopenharmony_ci| -------- | ------- | ----- | ---- | ---------- | 25e41f4b71Sopenharmony_ci| checked | boolean | false | No | Whether the component is checked or not. | 26e41f4b71Sopenharmony_ci| showtext | boolean | false | No | Whether the component displays text. | 27e41f4b71Sopenharmony_ci| texton | string | "On" | No | Text displayed when the component is checked. | 28e41f4b71Sopenharmony_ci| textoff | string | "Off" | No | Text displayed when the component is not checked.| 29e41f4b71Sopenharmony_ci 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ci## Styles 32e41f4b71Sopenharmony_ci 33e41f4b71Sopenharmony_ci 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_ciIn addition to the [universal styles](js-components-common-styles.md), the following styles are supported. 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_ci| Name | Type | Default Value | Mandatory | Description | 38e41f4b71Sopenharmony_ci| ------------- | -------------------------- | ---------- | ---- | ---------------------------------------- | 39e41f4b71Sopenharmony_ci| texton-color | <color> | \#000000 | No | Text color displayed when the component is checked. This attribute is available only when **texton** and **textoff** are set. | 40e41f4b71Sopenharmony_ci| textoff-color | <color> | \#000000 | No | Text color displayed when the component is not checked. This attribute is available only when **texton** and **textoff** are set. | 41e41f4b71Sopenharmony_ci| text-padding | number | 0px | No | Distance between the two sides of the longest text in **texton** and **textoff** and the border of the slider. | 42e41f4b71Sopenharmony_ci| font-size | <length> | - | No | Font size. This attribute is available only when **texton** and **textoff** are set. | 43e41f4b71Sopenharmony_ci| allow-scale | boolean | true | No | Whether the font size changes with the system's font size settings.<br>If the **config-changes** tag of **fontSize** is configured for abilities in the **config.json** file, the setting takes effect without application restart.| 44e41f4b71Sopenharmony_ci| font-style | string | normal | No | Font style. This attribute is available only when **texton** and **textoff** are set. For details, see [font-style](js-components-basic-text.md#styles) of the **\<text>** component.| 45e41f4b71Sopenharmony_ci| font-weight | number \| string | normal | No | Font weight. This attribute is available only when **texton** and **textoff** are set. For details, see [font-weight](js-components-basic-text.md#styles) of the **\<text>** component.| 46e41f4b71Sopenharmony_ci| font-family | string | sans-serif | No | Font family, in which fonts are separated by commas (,). Each font is set using a font name or font family name. The first font in the family or the specified [custom font](js-components-common-customizing-font.md) is used for the text. This attribute is available only when **texton** and **textoff** are set.| 47e41f4b71Sopenharmony_ci 48e41f4b71Sopenharmony_ci 49e41f4b71Sopenharmony_ci## Events 50e41f4b71Sopenharmony_ci 51e41f4b71Sopenharmony_ciIn addition to the [universal events](js-components-common-events.md), the following events are supported. 52e41f4b71Sopenharmony_ci 53e41f4b71Sopenharmony_ci| Name | Parameter | Description | 54e41f4b71Sopenharmony_ci| ------ | ---------------------------------------- | ------------- | 55e41f4b71Sopenharmony_ci| change | { checked: checkedValue } | Triggered when the **checked** state changes.| 56e41f4b71Sopenharmony_ci 57e41f4b71Sopenharmony_ci## Methods 58e41f4b71Sopenharmony_ci 59e41f4b71Sopenharmony_ciThe [universal methods](js-components-common-methods.md) are supported. 60e41f4b71Sopenharmony_ci 61e41f4b71Sopenharmony_ci## Example 62e41f4b71Sopenharmony_ci 63e41f4b71Sopenharmony_ci```html 64e41f4b71Sopenharmony_ci<!-- xxx.hml --> 65e41f4b71Sopenharmony_ci<div class="container"> 66e41f4b71Sopenharmony_ci <switch @change="normalswitchChange"> 67e41f4b71Sopenharmony_ci </switch> 68e41f4b71Sopenharmony_ci <switch class="switch" showtext="true" texton="On" textoff="Off" @change="switchChange"> 69e41f4b71Sopenharmony_ci </switch> 70e41f4b71Sopenharmony_ci <switch class="switch text" showtext="true" texton="Switch on" textoff="Switch off" checked="true" @change="switchChange"> 71e41f4b71Sopenharmony_ci </switch> 72e41f4b71Sopenharmony_ci</div> 73e41f4b71Sopenharmony_ci``` 74e41f4b71Sopenharmony_ci 75e41f4b71Sopenharmony_ci```css 76e41f4b71Sopenharmony_ci/* xxx.css */ 77e41f4b71Sopenharmony_ci.container { 78e41f4b71Sopenharmony_ci display: flex; 79e41f4b71Sopenharmony_ci justify-content: center; 80e41f4b71Sopenharmony_ci align-items: center; 81e41f4b71Sopenharmony_ci} 82e41f4b71Sopenharmony_ci.switch { 83e41f4b71Sopenharmony_ci texton-color: red; 84e41f4b71Sopenharmony_ci textoff-color: forestgreen; 85e41f4b71Sopenharmony_ci} 86e41f4b71Sopenharmony_ci.text { 87e41f4b71Sopenharmony_ci text-padding: 20px; 88e41f4b71Sopenharmony_ci font-size: 30px; 89e41f4b71Sopenharmony_ci font-weight: 700; 90e41f4b71Sopenharmony_ci} 91e41f4b71Sopenharmony_ci``` 92e41f4b71Sopenharmony_ci 93e41f4b71Sopenharmony_ci```js 94e41f4b71Sopenharmony_ci// xxx.js 95e41f4b71Sopenharmony_ciimport promptAction from '@ohos.promptAction'; 96e41f4b71Sopenharmony_ciexport default { 97e41f4b71Sopenharmony_ci data: { 98e41f4b71Sopenharmony_ci title: 'World' 99e41f4b71Sopenharmony_ci }, 100e41f4b71Sopenharmony_ci switchChange(e) { 101e41f4b71Sopenharmony_ci if (e.checked) { 102e41f4b71Sopenharmony_ci promptAction.showToast({ 103e41f4b71Sopenharmony_ci message: "Switch on." 104e41f4b71Sopenharmony_ci }); 105e41f4b71Sopenharmony_ci } else { 106e41f4b71Sopenharmony_ci promptAction.showToast({ 107e41f4b71Sopenharmony_ci message: "Switch off." 108e41f4b71Sopenharmony_ci }); 109e41f4b71Sopenharmony_ci } 110e41f4b71Sopenharmony_ci }, 111e41f4b71Sopenharmony_ci normalswitchChange(e) { 112e41f4b71Sopenharmony_ci if (e.checked) { 113e41f4b71Sopenharmony_ci promptAction.showToast({ 114e41f4b71Sopenharmony_ci message: "switch on" 115e41f4b71Sopenharmony_ci }); 116e41f4b71Sopenharmony_ci } else { 117e41f4b71Sopenharmony_ci promptAction.showToast({ 118e41f4b71Sopenharmony_ci message: "switch off" 119e41f4b71Sopenharmony_ci }); 120e41f4b71Sopenharmony_ci } 121e41f4b71Sopenharmony_ci } 122e41f4b71Sopenharmony_ci} 123e41f4b71Sopenharmony_ci``` 124e41f4b71Sopenharmony_ci 125e41f4b71Sopenharmony_ci 126