1# Button 2 3 4The **Button** component is usually activated by user clicks to perform a specific action. Buttons are classified as capsule, circle, or normal buttons. When used as a container, the **Button** component accepts child components such as text and images. For details, see [Button](../reference/apis-arkui/arkui-ts/ts-basic-components-button.md). 5 6 7## Creating a Button 8 9You can create a button that contains or does not contain child components. 10 11 12- Create a button that does not contain child components. 13 14 ```ts 15 Button(label?: ResourceStr, options?: { type?: ButtonType, stateEffect?: boolean }) 16 ``` 17 18 In this API, **label** indicates the button text, **type** indicates the button type, and **stateEffect** specifies whether to enable the pressed effect on the click of the button. 19 20 ```ts 21 Button('Ok', { type: ButtonType.Normal, stateEffect: true }) 22 .borderRadius(8) 23 .backgroundColor(0x317aff) 24 .width(90) 25 .height(40) 26 ``` 27 28  29 30 31- Create a button that contains a single child component. 32 33 ```ts 34 Button(options?: {type?: ButtonType, stateEffect?: boolean}) 35 ``` 36 37 The child component contained can either be a basic component or a container component. 38 39 ```ts 40 Button({ type: ButtonType.Normal, stateEffect: true }) { 41 Row() { 42 Image($r('app.media.loading')).width(20).height(40).margin({ left: 12 }) 43 Text('loading').fontSize(12).fontColor(0xffffff).margin({ left: 5, right: 12 }) 44 }.alignItems(VerticalAlign.Center) 45 }.borderRadius(8).backgroundColor(0x317aff).width(90).height(40) 46 ``` 47 48  49 50 51## Setting the Button Type 52 53Use the **type** parameter to set the button type to **Capsule**, **Circle**, or **Normal**. 54 55 56- Capsule button (default type) 57 58 Buttons of this type have rounded corners whose radius is automatically set to half of the button height. The rounded corners cannot be reset through the **borderRadius** attribute. 59 60 ```ts 61 Button('Disable', { type: ButtonType.Capsule, stateEffect: false }) 62 .backgroundColor(0x317aff) 63 .width(90) 64 .height(40) 65 ``` 66 67  68 69 70- Circle button 71 72 Buttons of this type are round. The rounded corners cannot be reset through the **borderRadius** attribute. 73 74 ```ts 75 Button('Circle', { type: ButtonType.Circle, stateEffect: false }) 76 .backgroundColor(0x317aff) 77 .width(90) 78 .height(90) 79 ``` 80 81  82 83- Normal button 84 85 Buttons of this type have rounded corners set to 0. The rounded corners can be reset through the **borderRadius** attribute. 86 87 ```ts 88 Button('Ok', { type: ButtonType.Normal, stateEffect: true }) 89 .borderRadius(8) 90 .backgroundColor(0x317aff) 91 .width(90) 92 .height(40) 93 ``` 94 95  96 97 98## Setting Styles 99 100- Set the border radius. 101 102 You can use universal attributes to define the button styles. For example, you can use the **borderRadius** attribute to set the border radius. 103 104 ```ts 105 Button('circle border', { type: ButtonType.Normal }) 106 .borderRadius(20) 107 .height(40) 108 ``` 109 110  111 112 113- Set the text style. 114 115 Add text style attributes for the button. 116 117 ```ts 118 Button('font style', { type: ButtonType.Normal }) 119 .fontSize(20) 120 .fontColor(Color.Pink) 121 .fontWeight(800) 122 ``` 123 124  125 126 127- Set the background color. 128 129 Add the **backgroundColor** attribute for the button. 130 131 ```ts 132 Button('background color').backgroundColor(0xF55A42) 133 ``` 134 135  136 137 138- Assign a function to the button. 139 140 In this example, the delete function is assigned to the button. 141 142 ```ts 143 let MarLeft: Record<string, number> = { 'left': 20 } 144 Button({ type: ButtonType.Circle, stateEffect: true }) { 145 Image($r('app.media.ic_public_delete_filled')).width(30).height(30) 146 }.width(55).height(55).margin(MarLeft).backgroundColor(0xF55A42) 147 ``` 148 149  150 151 152## Adding Events 153 154The **Button** component is usually used to trigger actions. You can bind the **onClick** event to the button to have it respond with custom behavior after being clicked. 155 156```ts 157Button('Ok', { type: ButtonType.Normal, stateEffect: true }) 158 .onClick(()=>{ 159 console.info('Button onClick') 160 }) 161``` 162 163 164## Example 165 166- Using the button for startup 167 168 You can use the button for any UI element that involves the startup operation. The button triggers the predefined event based on the user's operation. For example, you can use a button in the **List** container to redirect the user to another page. 169 170 ```ts 171 // xxx.ets 172 @Entry 173 @Component 174 struct ButtonCase1 { 175 pathStack: NavPathStack = new NavPathStack(); 176 177 @Builder 178 PageMap(name: string) { 179 if (name === "first_page") { 180 pageOneTmp() 181 } else if (name === "second_page") { 182 pageTwoTmp() 183 } else if (name === "third_page") { 184 pageThreeTmp() 185 } 186 } 187 188 build() { 189 Navigation(this.pathStack) { 190 List({ space: 4 }) { 191 ListItem() { 192 Button("First").onClick(() => { 193 this.pathStack.pushPath({ name: "first_page"}) 194 }) 195 .width('100%') 196 } 197 198 ListItem() { 199 Button("Second").onClick(() => { 200 this.pathStack.pushPath({ name: "second_page"}) 201 }) 202 .width('100%') 203 } 204 205 ListItem() { 206 Button("Third").onClick(() => { 207 this.pathStack.pushPath({ name: "third_page"}) 208 }) 209 .width('100%') 210 } 211 } 212 .listDirection(Axis.Vertical) 213 .backgroundColor(0xDCDCDC).padding(20) 214 } 215 .mode(NavigationMode.Stack) 216 .navDestination(this.PageMap) 217 } 218 } 219 220 // pageOne 221 @Component 222 export struct pageOneTmp { 223 pathStack: NavPathStack = new NavPathStack(); 224 225 build() { 226 NavDestination() { 227 Column() { 228 Text("first_page") 229 }.width('100%').height('100%') 230 }.title("pageOne") 231 .onBackPressed(() => { 232 const popDestinationInfo = this.pathStack.pop() // Pop the top element out of the navigation stack. 233 console.log('pop' + 'Return value' + JSON.stringify(popDestinationInfo)) 234 return true 235 }) 236 .onReady((context: NavDestinationContext) => { 237 this.pathStack = context.pathStack 238 }) 239 } 240 } 241 242 // pageTwo 243 @Component 244 export struct pageTwoTmp { 245 pathStack: NavPathStack = new NavPathStack(); 246 247 build() { 248 NavDestination() { 249 Column() { 250 Text("second_page") 251 }.width('100%').height('100%') 252 }.title("pageTwo") 253 .onBackPressed(() => { 254 const popDestinationInfo = this.pathStack.pop() // Pop the top element out of the navigation stack. 255 console.log('pop' + 'Return value' + JSON.stringify(popDestinationInfo)) 256 return true 257 }) 258 .onReady((context: NavDestinationContext) => { 259 this.pathStack = context.pathStack 260 }) 261 } 262 } 263 264 // pageThree 265 @Component 266 export struct pageThreeTmp { 267 pathStack: NavPathStack = new NavPathStack(); 268 269 build() { 270 NavDestination() { 271 Column() { 272 Text("third_page") 273 }.width('100%').height('100%') 274 }.title("pageThree") 275 .onBackPressed(() => { 276 const popDestinationInfo = this.pathStack.pop() // Pop the top element out of the navigation stack. 277 console.log('pop' + 'Return value' + JSON.stringify(popDestinationInfo)) 278 return true 279 }) 280 .onReady((context: NavDestinationContext) => { 281 this.pathStack = context.pathStack 282 }) 283 } 284 } 285 ``` 286 287  288 289 290- Using the button for submitting forms 291 292 On the user login/registration page, you can use a button to submit a login or registration request. 293 294 ```ts 295 // xxx.ets 296 @Entry 297 @Component 298 struct ButtonCase2 { 299 build() { 300 Column() { 301 TextInput({ placeholder: 'input your username' }).margin({ top: 20 }) 302 TextInput({ placeholder: 'input your password' }).type(InputType.Password).margin({ top: 20 }) 303 Button('Register').width(300).margin({ top: 20 }) 304 .onClick(() => { 305 // Operation 306 }) 307 }.padding(20) 308 } 309 } 310 ``` 311 312  313 314- Configuring the button to float 315 316 The button can remain floating when the user swipes on the screen. 317 318 ```ts 319 // xxx.ets 320 @Entry 321 @Component 322 struct HoverButtonExample { 323 private arr: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 324 build() { 325 Stack() { 326 List({ space: 20, initialIndex: 0 }) { 327 ForEach(this.arr, (item:number) => { 328 ListItem() { 329 Text('' + item) 330 .width('100%').height(100).fontSize(16) 331 .textAlign(TextAlign.Center).borderRadius(10).backgroundColor(0xFFFFFF) 332 } 333 }, (item:number) => item.toString()) 334 }.width('90%') 335 Button() { 336 Image($r('app.media.ic_public_add')) 337 .width(50) 338 .height(50) 339 } 340 .width(60) 341 .height(60) 342 .position({x: '80%', y: 600}) 343 .shadow({radius: 10}) 344 .onClick(() => { 345 // Operation 346 }) 347 } 348 .width('100%') 349 .height('100%') 350 .backgroundColor(0xDCDCDC) 351 .padding({ top: 5 }) 352 } 353 } 354 ``` 355 356  357