openharmony 鸿蒙 js-components-basic-switch

2025-06-12 浏览 (1)

switch

NOTE

This component is supported since API version 4. Updates will be marked with a superscript to indicate their earliest API version.

The <switch> component is used to enable or disable a function.

Required Permissions

None

Child Components

Not supported

Attributes

In addition to the universal attributes, the following attributes are supported.

NameTypeDefault ValueMandatoryDescription
checkedbooleanfalseNoWhether the component is checked or not.
showtextbooleanfalseNoWhether the component displays text.
textonstring"On"NoText displayed when the component is checked.
textoffstring"Off"NoText displayed when the component is not checked.

Styles

In addition to the universal styles, the following styles are supported.

NameTypeDefault ValueMandatoryDescription
texton-color<color>#000000NoText color displayed when the component is checked. This attribute is available only when texton and textoff are set.
textoff-color<color>#000000NoText color displayed when the component is not checked. This attribute is available only when texton and textoff are set.
text-paddingnumber0pxNoDistance between the two sides of the longest text in texton and textoff and the border of the slider.
font-size<length>-NoFont size. This attribute is available only when texton and textoff are set.
allow-scalebooleantrueNoWhether the font size changes with the system's font size settings.
If the config-changes tag of fontSize is configured for abilities in the config.json file, the setting takes effect without application restart.
font-stylestringnormalNoFont style. This attribute is available only when texton and textoff are set. For details, see font-style of the <text> component.
font-weightnumber |stringnormalNoFont weight. This attribute is available only when texton and textoff are set. For details, see font-weight of the <text> component.
font-familystringsans-serifNoFont 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 is used for the text. This attribute is available only when texton and textoff are set.

Events

In addition to the universal events, the following events are supported.

NameParameterDescription
change{ checked: checkedValue }Triggered when the checked state changes.

Methods

The universal methods are supported.

Example

<!-- xxx.hml -->
<div class="container">
    <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>
</div>
/* xxx.css */
.container {
    display: flex;
    justify-content: center;
    align-items: center;
}
.switch {
    texton-color: red;
    textoff-color: forestgreen;
}
.text {
    text-padding: 20px;
    font-size: 30px;
    font-weight: 700;
}
// xxx.js
import promptAction from '@ohos.promptAction';
export default {
    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"
            });
        }
    }
}

en-us_image_0000001152862510

你可能感兴趣的鸿蒙文章

harmony 鸿蒙JavaScript-compatible Web-like Development Paradigm (ArkUI.Full)

harmony 鸿蒙Data Type Attributes

harmony 鸿蒙button

harmony 鸿蒙chart

harmony 鸿蒙divider

harmony 鸿蒙image-animator

harmony 鸿蒙image

harmony 鸿蒙input

harmony 鸿蒙label

harmony 鸿蒙marquee

  • 所属分类: 后端技术
  • 本文标签: 鸿蒙 软件
  • 版权声明: 本文链接 https://seaxiang.com/blog/iwhC2j