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