1# \<button> Development 2 3The **\<button>** component can be used to set a capsule, circle, text, arc, or download button. For details, see [button](../reference/arkui-js/js-components-basic-button.md). 4 5 6## Creating a \<button> Component 7 8Create a **\<button>** component in the .hml file under **pages/index**. 9 10```html 11<!-- xxx.hml --> 12<div class="container"> 13 <button type="capsule" value="Capsule button"></button> 14</div> 15``` 16 17```css 18/* xxx.css */ 19.container { 20 width: 100%; 21 height: 100%; 22 flex-direction: column; 23 justify-content: center; 24 align-items: center; 25 background-color: #F1F3F5; 26} 27``` 28 29![en-us_image_0000001267887821](figures/en-us_image_0000001267887821.png) 30 31 32## Setting the Button Type 33 34Set the **type** attribute of the **\<button>** component to **circle**, **text**, or any other supported value. 35 36 37```html 38<!-- xxx.hml --> 39<div class="container"> 40 <button class="circle" type="circle" >+</button> 41 <button class="text" type="text"> button</button> 42</div> 43``` 44 45 46```css 47/* xxx.css */ 48.container { 49 width: 100%; 50 height: 100%; 51 background-color: #F1F3F5; 52 flex-direction: column; 53 align-items: center; 54 justify-content: center; 55} 56.circle { 57 font-size: 120px; 58 background-color: blue; 59 radius: 72px; 60} 61.text { 62 margin-top: 30px; 63 text-color: white; 64 font-size: 30px; 65 font-style: normal; 66 background-color: blue; 67 width: 50%; 68 height: 100px; 69} 70``` 71 72 73![en-us_image_0000001222967744](figures/en-us_image_0000001222967744.png) 74 75 76> **NOTE** 77> 78>If the icon used by the **\<button>** component is from the cloud, you must declare the **ohos.permission.INTERNET** permission in the **config.json** file under the **resources** folder. 79 80 81Sample code for declaring the **ohos.permission.INTERNET** permission in the **config.json** file under the **resources** folder: 82 83 84``` 85<!-- config.json --> 86"module": { 87 "reqPermissions": [{ 88 "name": "ohos.permission.INTERNET" 89 }], 90} 91``` 92 93 94## Showing the Download Progress 95 96Add the **progress** method to the **\<button>** component to display the download progress in real time. 97 98```html 99<!-- xxx.hml --> 100<div class="container"> 101 <button class="button download" type="download" id="download-btn" onclick="setProgress">{{downloadText}}</button> 102</div> 103``` 104 105```css 106/* xxx.css */ 107.container { 108 width: 100%; 109 height: 100%; 110 background-color: #F1F3F5; 111 flex-direction: column; 112 align-items: center; 113 justify-content: center; 114} 115.download { 116 width: 280px; 117 text-color: white; 118 background-color: #007dff; 119} 120``` 121 122```js 123// xxx.js 124import promptAction from '@ohos.promptAction'; 125export default { 126 data: { 127 percent: 0, 128 downloadText: "Download", 129 isPaused: true, 130 intervalId : null, 131 }, 132 start(){ 133 this.intervalId = setInterval(()=>{ 134 if(this.percent <100){ 135 this.percent += 1; 136 this.downloadText = this.percent+ "%"; 137 } else{ 138 promptAction.showToast({ 139 message: "Download succeeded." 140 }) 141 this.paused() 142 this.downloadText = "Download"; 143 this.percent = 0; 144 this.isPaused = true; 145 } 146 },100) 147 }, 148 paused(){ 149 clearInterval(this.intervalId); 150 this.intervalId = null; 151 }, 152 setProgress(e) { 153 if(this.isPaused){ 154 promptAction.showToast({ 155 message: "Started Downloading" 156 }) 157 this.start(); 158 this.isPaused = false; 159 }else{ 160 promptAction.showToast({ 161 message: "Paused." 162 }) 163 this.paused(); 164 this.isPaused = true; 165 } 166 } 167} 168``` 169 170![en-us_image_0000001223287652](figures/en-us_image_0000001223287652.gif) 171 172> **NOTE** 173> 174> The **setProgress** method supports only buttons of the download type. 175 176 177## Example Scenario 178 179Switch between the button types for different types of text. 180 181 182```html 183<!-- xxx.hml --> 184<div class="container"> 185 <div class="input-item"> 186 <input class="input-text" id="change" type="{{mytype}}" placeholder="{{myholder}}" 187 style="background-color:{{mystyle1}}; 188 placeholder-color:{{mystyle2}};flex-grow:{{myflex}};"name="{{myname}}" value="{{myvalue}}"></input> 189 </div> 190 <div class="input-item"> 191 <div class="doc-row"> 192 <input type="button" class="select-button color-3" value="text" onclick="changetype3"></input> 193 <input type="button" class="select-button color-3" value="data" onclick="changetype4"></input> 194 </div> 195 </div> 196</div> 197``` 198 199 200```css 201/* xxx.css */ 202.container { 203 flex-direction: column; 204 align-items: center; 205 background-color: #F1F3F5; 206} 207.input-item { 208 margin-bottom: 80px; 209 flex-direction: column; 210} 211.doc-row { 212 justify-content: center; 213 margin-left: 30px; 214 margin-right: 30px; 215 justify-content: space-around; 216} 217.input-text { 218 height: 80px; 219 line-height: 80px; 220 padding-left: 30px; 221 padding-right: 30px; 222 margin-left: 30px; 223 margin-right: 30px; 224 margin-top:100px; 225 border: 3px solid; 226 border-color: #999999; 227 font-size: 30px; 228 background-color: #ffffff; 229 font-weight: 400; 230} 231.select-button { 232 width: 35%; 233 text-align: center; 234 height: 70px; 235 padding-top: 10px; 236 padding-bottom: 10px; 237 margin-top: 30px; 238 font-size: 30px; 239 color: #ffffff; 240} 241.color-3 { 242 background-color: #0598db;; 243} 244``` 245 246 247```js 248// xxx.js 249export default { 250 data: { 251 myflex: '', 252 myholder: 'Enter text.', 253 myname: '', 254 mystyle1: "#ffffff", 255 mystyle2: "#ff0000", 256 mytype: 'text', 257 myvalue: '', 258 }, 259 onInit() { 260 }, 261 changetype3() { 262 this.myflex = ''; 263 this.myholder = 'Enter text.'; 264 this.myname = ''; 265 this.mystyle1 = "#ffffff"; 266 this.mystyle2 = "#FF0000"; 267 this.mytype = 'text'; 268 this.myvalue = ''; 269 }, 270 changetype4() { 271 this.myflex = ''; 272 this.myholder = 'Enter a date.'; 273 this.myname = ''; 274 this.mystyle1 = "#ffffff"; 275 this.mystyle2 = "#FF0000"; 276 this.mytype = 'date'; 277 this.myvalue = ''; 278 }, 279} 280``` 281 282 283![en-us_image_0000001222967740](figures/en-us_image_0000001222967740.gif) 284