1# <switch> Development 2 3 4The **<switch>** component is used to switch between the on and off states. For details, see [switch](../reference/arkui-js/js-components-basic-switch.md). 5 6 7## Creating a <switch> Component 8 9Create a **<switch>** component in the .hml file under **pages/index**. 10 11 12```html 13<div class="container"> 14 <switch></switch> 15</div> 16``` 17 18 19```css 20/* xxx.css */ 21.container { 22 flex-direction: column; 23 background-color: #F1F3F5; 24} 25``` 26 27 28 29 30## Adding Attributes and Methods 31 32Use the **textoff** and **showtext** attributes to set the status when text is selected and unselected. Set the **checked** attribute to **true** (indicating that the component is on). Add the **change** event that is triggered when the component status changes. After the event is triggered, the **switchChange** function is executed to obtain the current component status (on or off). 33 34```html 35<!-- xxx.hml --> 36<div class="container"> 37 <switch showtext="true" texton="open" textoff="close" checked="true" @change="switchChange"></switch> 38</div> 39``` 40 41 42``` 43/* xxx.css */ 44.container { 45 width: 100%; 46 height: 100%; 47 display: flex; 48 justify-content: center; 49 align-items: center; 50 background-color: #F1F3F5; 51} 52switch{ 53 texton-color: #002aff; 54 textoff-color: silver; 55 text-padding: 20px; 56 font-size: 50px; 57} 58``` 59 60 61```css 62// xxx.js 63import prompt from '@system.prompt'; 64export default { 65 switchChange(e){ 66 if(e.checked){ 67 prompt.showToast({ 68 message: "open" 69 }); 70 }else{ 71 prompt.showToast({ 72 message: "close" 73 }); 74 } 75 } 76} 77``` 78 79 80 81 82 83> **NOTE** 84> 85> The text set by **texton** and **textoff** takes effect only when **showtext** is set to **true**. 86 87 88## Example Scenario 89 90Turn on the switch and the default delivery address is used. When the switch is turned off, the address selection button is displayed on the page. Clicking the button can change the delivery address. 91 92 Implementation method: Create a **<switch>** component, set the **checked** attribute to **true**, and change the delivery address through data binding. Set the **display** attribute (the default value is **none**). When the switch is turned off and the **display** attribute is set to **flex**, the address module is displayed and clicking the button can change the color. 93 94```html 95<!-- xxx.hml --> 96<div class="container"> 97 <div class="change"> 98 <text>Choose default address:</text> 99 <switch showtext="true" texton="on" textoff="off" checked="true" @change="switchChange"></switch> 100 </div> 101 <div class="content"> 102 <text class="address"><span>Shipping address:</span><span class="textSpan">{{address}}</span></text> 103 </div> 104 <div class="myAddress" style="display: {{addressDisplay}};"> 105 <text style="font-size: 30px;margin-bottom: 50px;">Choose an address:</text> 106 <text class="addressText" style="background-color: {{item == address?'#0fabe7':''}};color: {{item == address?'white':'black'}};" 107 for="item in addressList"@click="changeAddress({{$idx}}})">{{item}}</text> 108 </div> 109</div> 110``` 111 112 113```css 114/* xxx.css */ 115.container { 116 width: 100%; 117 height: 100%; 118 background-color: #F1F3F5; 119 flex-direction: column; 120 padding: 50px; 121} 122.change{ 123 margin-top: 20%; 124 width: 100%; 125 justify-content: center; 126} 127switch{ 128 texton-color: #002aff; 129 textoff-color: silver; 130 text-padding: 20px; 131} 132.content{ 133 width: 70%; 134 text-align: center; 135 flex-direction: column; 136 border: 1px solid #002aff; 137 margin-left: 15%; 138 text-align: center; 139} 140.address{ 141 width: 100%; 142 height: 100px; 143 line-height: 100px; 144 text-align: center; 145 font-size: 28px; 146 margin-bottom: 50px; 147} 148.textSpan{ 149 color: #0aa9f1; 150} 151.myAddress{ 152 flex-direction: column; 153 margin-top: 50px; 154} 155.addressText{ 156 margin-left: 35%; 157 width: 30%; 158 height: 75px; 159 text-align: center; 160 color: white; 161 margin-bottom: 30px; 162 border-radius: 10px; 163 border: 1px solid #0fabe7; 164} 165``` 166 167 168```js 169// xxx.js 170import prompt from '@system.prompt'; 171export default { 172 data:{ 173 address: '', 174 addressDisplay: 'none', 175 addressList: ['family','company','commissary'], 176 }, 177 onInit(){ 178 // Initialize the default address to the first one in the address list. 179 this.address = this.addressList[0]; 180 }, 181 switchChange(e){ 182 if(e.checked){ 183 this.addressDisplay = "none"; 184 }else{ 185 this.addressDisplay = "flex"; 186 } 187 }, 188 changeAddress(i){ 189 this.address= this.addressList[i]; 190 } 191} 192``` 193 194 195