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