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