1# switch 2<!--Kit: ArkUI--> 3<!--Subsystem: ArkUI--> 4<!--Owner: @houguobiao--> 5<!--Designer: @houguobiao--> 6<!--Tester: @lxl007--> 7<!--Adviser: @HelloCrease--> 8 9> **说明:** 10> 从API version 4开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。 11 12开关选择器,通过开关,开启或关闭某个功能。 13 14## 权限列表 15 16无 17 18 19## 子组件 20 21不支持。 22 23 24## 属性 25 26除支持[通用属性](js-components-common-attributes.md)外,还支持如下属性: 27 28| 名称 | 类型 | 默认值 | 必填 | 描述 | 29| -------- | ------- | ----- | ---- | ---------- | 30| checked | boolean | false | 否 | 是否选中。 true表示选中,false表示未选中。 | 31| showtext | boolean | false | 否 | 是否显示文本。true表示显示文本,false表示不显示文本。 | 32| texton | string | "On" | 否 | 选中时显示的文本。 | 33| textoff | string | "Off" | 否 | 未选中时显示的文本。 | 34 35 36## 样式 37 38 39 40除支持[通用样式](js-components-common-styles.md)外,还支持如下样式: 41 42| 名称 | 类型 | 默认值 | 必填 | 描述 | 43| ------------- | -------------------------- | ---------- | ---- | ---------------------------------------- | 44| texton-color | <color> | \#000000 | 否 | 选中时显示的文本颜色,仅设置texton和textoff生效。 | 45| textoff-color | <color> | \#000000 | 否 | 未选中时显示的文本颜色,仅设置texton和textoff生效。 | 46| text-padding | number | 0px | 否 | texton/textoff中最长文本两侧距离滑块边界的距离。 | 47| font-size | <length> | - | 否 | 文本尺寸,仅设置texton和textoff生效。 | 48| allow-scale | boolean | true | 否 | 文本尺寸是否跟随系统设置字体缩放尺寸进行放大缩小。true表示跟随系统放大缩小,false表示不跟随系统放大缩小。<br/>如果在config描述文件中针对ability配置了fontSize的config-changes标签,则应用不会重启而直接生效。 | 49| font-style | string | normal | 否 | 字体样式,仅设置texton和textoff生效。见text组件[font-style的样式属性](js-components-basic-text.md#样式)。 | 50| font-weight | number \| string | normal | 否 | 字体粗细,仅设置texton和textoff生效。见text组件的[font-weight的样式属性](js-components-basic-text.md#样式)。 | 51| font-family | string | sans-serif | 否 | 字体列表,用逗号分隔,每个字体用字体名或者字体族名设置。列表中第一个系统中存在的或者通过[自定义字体](js-components-common-customizing-font.md)指定的字体,会被选中作为文本的字体。仅设置texton和textoff生效。 | 52 53 54## 事件 55 56除支持[通用事件](js-components-common-events.md)外,还支持如下事件: 57 58| 名称 | 参数 | 描述 | 59| ------ | ---------------------------------------- | ------------- | 60| change | { checked: checkedValue } | 选中状态改变时触发该事件。 | 61 62## 方法 63 64支持[通用方法](js-components-common-methods.md)。 65 66## 示例 67 68```html 69<!-- xxx.hml --> 70<div class="container"> 71 <switch @change="normalSwitchChange"> 72 </switch> 73 <switch class="switch" showtext="true" texton="开启" textoff="关闭" @change="switchChange"> 74 </switch> 75 <switch class="switch text" showtext="true" texton="开启" textoff="关闭" checked="true" @change="switchChange"> 76 </switch> 77</div> 78``` 79 80```css 81/* xxx.css */ 82.container { 83 display: flex; 84 justify-content: center; 85 align-items: center; 86} 87.switch { 88 texton-color: red; 89 textoff-color: forestgreen; 90} 91.text { 92 text-padding: 20px; 93 font-size: 30px; 94 font-weight: 700; 95} 96``` 97 98```js 99// xxx.js 100import promptAction from '@ohos.promptAction'; 101export default { 102 data: { 103 title: 'World' 104 }, 105 switchChange(e) { 106 if (e.checked) { 107 promptAction.showToast({ 108 message: "打开开关" 109 }); 110 } else { 111 promptAction.showToast({ 112 message: "关闭开关" 113 }); 114 } 115 }, 116 normalSwitchChange(e) { 117 if (e.checked) { 118 promptAction.showToast({ 119 message: "switch on" 120 }); 121 } else { 122 promptAction.showToast({ 123 message: "switch off" 124 }); 125 } 126 } 127} 128``` 129 130 131