js-components-basic-switch.md 5.0 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 34


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

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


E
ester.zhou 已提交
49
## Events
Z
zengyawen 已提交
50

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

E
ester.zhou 已提交
53 54 55 56 57 58 59 60 61 62 63
| 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 已提交
64 65
<!-- xxx.hml -->
<div class="container">
E
ester.zhou 已提交
66 67 68 69 70 71
    <switch @change="normalswitchChange">
    </switch>
    <switch class="switch" showtext="true" texton="On" textoff="Off" @change="switchChange">
    </switch>
    <switch class="switch text" showtext="true" texton="Switch on" textoff="Switch off" checked="true" @change="switchChange">
    </switch>
Z
zengyawen 已提交
72 73 74
</div>
```

E
ester.zhou 已提交
75
```css
Z
zengyawen 已提交
76 77
/* xxx.css */
.container {
E
ester.zhou 已提交
78 79 80 81 82 83 84
    display: flex;
    justify-content: center;
    align-items: center;
}
.switch {
    texton-color: red;
    textoff-color: forestgreen;
Z
zengyawen 已提交
85
}
E
ester.zhou 已提交
86 87 88 89
.text {
    text-padding: 20px;
    font-size: 30px;
    font-weight: 700;
Z
zengyawen 已提交
90 91 92
}
```

E
ester.zhou 已提交
93
```js
Z
zengyawen 已提交
94
// xxx.js
E
ester.zhou 已提交
95
import promptAction from '@ohos.promptAction';
Z
zengyawen 已提交
96
export default {
E
ester.zhou 已提交
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
    data: {
        title: 'World'
    },
    switchChange(e) {
        if (e.checked) {
            promptAction.showToast({
                message: "Switch on."
            });
        } else {
            promptAction.showToast({
                message: "Switch off."
            });
        }
    },
    normalswitchChange(e) {
        if (e.checked) {
            promptAction.showToast({
                message: "switch on"
            });
        } else {
            promptAction.showToast({
                message: "switch off"
            });
        }
Z
zengyawen 已提交
121 122 123 124
    }
}
```

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