js-components-basic-switch.md 4.4 KB
Newer Older
E
ester.zhou 已提交
1
# switch
Z
zengyawen 已提交
2

E
ester.zhou 已提交
3 4 5
>  **NOTE**
>
>  This component is supported since API version 4. Updates will be marked with a superscript to indicate their earliest API version.
Z
zengyawen 已提交
6

E
ester.zhou 已提交
7 8 9
The **\<switch>** component is used to enable or disable a function.

## Required Permissions
Z
zengyawen 已提交
10 11 12 13

None


E
ester.zhou 已提交
14
## Child Components
Z
zengyawen 已提交
15

E
ester.zhou 已提交
16
Not supported
Z
zengyawen 已提交
17 18


E
ester.zhou 已提交
19
## Attributes
Z
zengyawen 已提交
20

E
ester.zhou 已提交
21
In addition to the [universal attributes](../arkui-js/js-components-common-attributes.md), the following attributes are supported.
Z
zengyawen 已提交
22

E
ester.zhou 已提交
23 24 25 26 27 28
| Name      | Type     | Default Value  | Mandatory  | Description        |
| -------- | ------- | ----- | ---- | ---------- |
| checked  | boolean | false | No   | Whether the component is checked or not.     |
| showtext | boolean | false | No   | Whether the component displays text.   |
| texton   | string  | "On"  | No   | Text displayed when the component is checked. |
| textoff  | string  | "Off" | No   | Text displayed when the component is not checked.|
Z
zengyawen 已提交
29 30


E
ester.zhou 已提交
31
## Styles
Z
zengyawen 已提交
32

E
ester.zhou 已提交
33
In addition to the [universal styles](../arkui-js/js-components-common-styles.md), the following styles are supported.
Z
zengyawen 已提交
34

E
ester.zhou 已提交
35 36 37 38 39 40 41 42 43 44
| Name                 | Type                        | Default Value       | Mandatory  | Description                                      |
| ------------------- | -------------------------- | ---------- | ---- | ---------------------------------------- |
| texton-color(Rich)  | &lt;color&gt;              | \#000000   | No   | Text color displayed when the component is checked.                             |
| textoff-color(Rich) | &lt;color&gt;              | \#000000   | No   | Text color displayed when the component is not checked.                            |
| text-padding(Rich)  | number                     | 0px        | No   | Distance between the two sides of the longest text in **texton** and **textoff** and the border of the slider.         |
| font-size(Rich)     | &lt;length&gt;             | -          | No   | Font size. This attribute is available only when **texton** and **textoff** are set.               |
| allow-scale(Rich)   | 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.|
| font-style(Rich)    | string                     | normal     | No   | Font style. This attribute is available only when **texton** and **textoff** are set. For details, see **font-style** of the [**\<text>**](../arkui-js/js-components-basic-text.md#styles) component.|
| font-weight(Rich)   | number \| string | normal     | No   | Font weight. This attribute is available only when **texton** and **textoff** are set. For details, see **font-weight** of the [**\<text>**](../arkui-js/js-components-basic-text.md#styles) component.|
| font-family(Rich)   | 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](../arkui-js/js-components-common-customizing-font.md) is used for the text. This attribute is available only when **texton** and **textoff** are set.|
Z
zengyawen 已提交
45 46


E
ester.zhou 已提交
47
## Events
Z
zengyawen 已提交
48

E
ester.zhou 已提交
49
In addition to the [universal events](../arkui-js/js-components-common-events.md), the following events are supported.
Z
zengyawen 已提交
50

E
ester.zhou 已提交
51 52 53 54 55 56 57 58 59 60 61
| Name    | Parameter                                      | Description           |
| ------ | ---------------------------------------- | ------------- |
| change | { checked: checkedValue } | Triggered when the **checked** state changes.|

## Methods

The [universal methods](../arkui-js/js-components-common-methods.md) are supported.

## Example

```html
Z
zengyawen 已提交
62 63 64 65 66 67 68
<!-- xxx.hml -->
<div class="container">
  <switch showtext="true" texton="On" textoff="Off" checked="true" @change="switchChange">
  </switch>
</div>
```

E
ester.zhou 已提交
69
```css
Z
zengyawen 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82
/* xxx.css */
.container {
  display: flex;
  justify-content: center;
  align-items: center;
}
switch{
  texton-color:#002aff;
  textoff-color:silver;
  text-padding:20px;
}
```

E
ester.zhou 已提交
83
```js
Z
zengyawen 已提交
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
// xxx.js
import prompt from '@system.prompt';
export default {
  data: {
    title: 'World'
  },
  switchChange(e){
    console.log(e.checked);
    if(e.checked){
      prompt.showToast({
        message: "Switch on."
      });
    }else{
      prompt.showToast({
        message: "Switch off."
      });
    }
  }
}
```

E
ester.zhou 已提交
105
![en-us_image_0000001152862510](figures/en-us_image_0000001152862510.gif)