ts-basic-components-toggle.md 4.2 KB
Newer Older
Z
zengyawen 已提交
1
# Toggle
Z
zengyawen 已提交
2

E
ester.zhou 已提交
3 4 5 6 7
The **\<Toggle>** component provides a clickable element in the check box, button, or switch type.

>  **NOTE**
>
>  This component is supported since API version 8. Updates will be marked with a superscript to indicate their earliest API version.
Z
zengyawen 已提交
8 9 10


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

None

Z
zengyawen 已提交
14 15

## Child Components
Z
zengyawen 已提交
16

E
ester.zhou 已提交
17
This component can contain child components only when **ToggleType** is set to **Button**.
Z
zengyawen 已提交
18

Z
zengyawen 已提交
19 20 21 22 23 24

## APIs

Toggle(options: { type: ToggleType, isOn?: boolean })

- Parameters
E
ester.zhou 已提交
25 26 27 28
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | type | ToggleType | Yes| Type of the toggle.|
  | isOn | boolean | No| Whether the toggle is turned on. The value **true** means that the toggle is turned on, and **false** means the opposite.<br/>Default value: **false** |
Z
zengyawen 已提交
29 30 31


- ToggleType enums
E
ester.zhou 已提交
32
  | Name| Description|
Z
zengyawen 已提交
33
  | -------- | -------- |
E
ester.zhou 已提交
34 35 36
  | Checkbox | Check box type.<br>> **NOTE**<br>> The default value of the universal attribute [padding](ts-universal-attributes-size.md) is as follows:<br>{<br> top: 14 vp,<br> right: 6 vp,<br> bottom: 14 vp,<br> left: 6 vp<br> } |
  | Button   | Button type. The set string, if any, will be displayed inside the button.      |
  | Switch   | Switch type.<br>> **NOTE**<br>> The default value of the universal attribute [padding](ts-universal-attributes-size.md) is as follows:<br>{<br> top: 12 vp,<br> right: 12 vp,<br> bottom: 12 vp,<br> left: 12 vp<br> } |
Z
zengyawen 已提交
37 38 39 40


## Attributes

E
ester.zhou 已提交
41
| Name| Parameter| Default Value| Description|
Z
zengyawen 已提交
42
| -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
43 44
| selectedColor | [ResourceColor](ts-types.md#resourcecolor) | - | Background color of the component when it is turned on.|
| switchPointColor | [ResourceColor](ts-types.md#resourcecolor) | - | Color of the circular slider when the component is of the **Switch** type.<br>> **NOTE**<br>> This attribute is valid only when **type** is set to **ToggleType.Switch**.|
Z
zengyawen 已提交
45 46 47 48


## Events

E
ester.zhou 已提交
49
| Name| Description|
Z
zengyawen 已提交
50
| -------- | -------- |
E
ester.zhou 已提交
51
| onChange(callback: (isOn: boolean) =&gt; void) | Triggered when the toggle status changes.|
Z
zengyawen 已提交
52 53 54 55


## Example

E
ester.zhou 已提交
56 57
```ts
// xxx.ets
Z
zengyawen 已提交
58 59 60
@Entry
@Component
struct ToggleExample {
E
ester.zhou 已提交
61

Z
zengyawen 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
  build() {
    Column({ space: 10 }) {
      Text('type: Switch').fontSize(12).fontColor(0xcccccc).width('90%')
      Flex({ justifyContent: FlexAlign.SpaceEvenly, alignItems: ItemAlign.Center }) {
        Toggle({ type: ToggleType.Switch, isOn: false })
          .selectedColor(0xed6f21)
          .switchPointColor(0xe5ffffff)
          .onChange((isOn: boolean) => {
            console.info('Component status:' + isOn)
          })

        Toggle({ type: ToggleType.Switch, isOn: true })
          .selectedColor(0x39a2db)
          .switchPointColor(0xe5ffffff)
          .onChange((isOn: boolean) => {
            console.info('Component status:' + isOn)
          })
      }

      Text('type: Checkbox').fontSize(12).fontColor(0xcccccc).width('90%')
      Flex({ justifyContent: FlexAlign.SpaceEvenly, alignItems: ItemAlign.Center }) {
        Toggle({ type: ToggleType.Checkbox, isOn: false })
          .size({ width: 28, height: 28 })
          .selectedColor(0xed6f21)
          .onChange((isOn: boolean) => {
            console.info('Component status:' + isOn)
          })

        Toggle({ type: ToggleType.Checkbox, isOn: true })
          .size({ width: 28, height: 28 })
          .selectedColor(0x39a2db)
          .onChange((isOn: boolean) => {
            console.info('Component status:' + isOn)
          })
      }

      Text('type: Button').fontSize(12).fontColor(0xcccccc).width('90%')
      Flex({ justifyContent: FlexAlign.SpaceEvenly, alignItems: ItemAlign.Center }) {
        Toggle({ type: ToggleType.Button, isOn: false }) {
          Text('status button').padding({ left: 12, right: 12 })
        }
        .selectedColor(0xed6f21)
        .onChange((isOn: boolean) => {
          console.info('Component status:' + isOn)
        })

        Toggle({ type: ToggleType.Button, isOn: true }) {
          Text('status button').padding({ left: 12, right: 12 })
        }
        .selectedColor(0x39a2db)
        .onChange((isOn: boolean) => {
          console.info('Component status:' + isOn)
        })
      }
    }.width('100%').padding(24)
  }
}
```

Z
zengyawen 已提交
121
![en-us_image_0000001211898522](figures/en-us_image_0000001211898522.gif)