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/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?: string, options?: { type?: ButtonType, stateEffect?: boolean }) 16 ``` 17 18 Creates a button that does not contain child components. In this API, **label** indicates the button text, **type** indicates the button type, and **stateEffect** specifies whether to set 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 child components. 32 33 ```ts 34 Button(options?: {type?: ButtonType, stateEffect?: boolean}) 35 ``` 36 37 Creates a button that contains a single child component, which can either be a [basic component](../reference/arkui-ts/ts-basic-components-blank.md) or a [container component](../reference/arkui-ts/ts-container-ability-component.md). 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 In general cases, 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 Button({ type: ButtonType.Circle, stateEffect: true }) { 144 Image($r('app.media.ic_public_delete_filled')).width(30).height(30) 145 }.width(55).height(55).margin({ left: 20 }).backgroundColor(0xF55A42) 146 ``` 147 148  149 150 151## Adding Events 152 153The **\<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. 154 155```ts 156Button('Ok', { type: ButtonType.Normal, stateEffect: true }) 157 .onClick(()=>{ 158 console.info('Button onClick') 159 }) 160``` 161 162 163## Example 164 165- Using the button for startup 166 167 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. 168 169 ```ts 170 // xxx.ets 171 import router from '@ohos.router'; 172 @Entry 173 @Component 174 struct ButtonCase1 { 175 build() { 176 List({ space: 4 }) { 177 ListItem() { 178 Button("First").onClick(() => { 179 router.pushUrl({ url: 'pages/first_page' }) 180 }) 181 .width('100%') 182 } 183 ListItem() { 184 Button("Second").onClick(() => { 185 router.pushUrl({ url: 'pages/second_page' }) 186 }) 187 .width('100%') 188 } 189 ListItem() { 190 Button("Third").onClick(() => { 191 router.pushUrl({ url: 'pages/third_page' }) 192 }) 193 .width('100%') 194 } 195 } 196 .listDirection(Axis.Vertical) 197 .backgroundColor(0xDCDCDC).padding(20) 198 } 199 } 200 ``` 201 202  203 204 205- Using the button for submitting forms 206 207 On the user login/registration page, you can use a button to submit a login or registration request. 208 209 ```ts 210 // xxx.ets 211 @Entry 212 @Component 213 struct ButtonCase2 { 214 build() { 215 Column() { 216 TextInput({ placeholder: 'input your username' }).margin({ top: 20 }) 217 TextInput({ placeholder: 'input your password' }).type(InputType.Password).margin({ top: 20 }) 218 Button('Register').width(300).margin({ top: 20 }) 219 .onClick(() => { 220 // Operation 221 }) 222 }.padding(20) 223 } 224 } 225 ``` 226 227  228 229- Configuring the button to float 230 231 The button can remain floating when the user swipes on the screen. 232 233 ```ts 234 // xxx.ets 235 @Entry 236 @Component 237 struct HoverButtonExample { 238 private arr: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 239 build() { 240 Stack() { 241 List({ space: 20, initialIndex: 0 }) { 242 ForEach(this.arr, (item) => { 243 ListItem() { 244 Text('' + item) 245 .width('100%').height(100).fontSize(16) 246 .textAlign(TextAlign.Center).borderRadius(10).backgroundColor(0xFFFFFF) 247 } 248 }, item => item) 249 }.width('90%') 250 Button() { 251 Image($r('app.media.ic_public_add')) 252 .width(50) 253 .height(50) 254 } 255 .width(60) 256 .height(60) 257 .position({x: '80%', y: 600}) 258 .shadow({radius: 10}) 259 .onClick(() => { 260 // Operation 261 }) 262 } 263 .width('100%') 264 .height('100%') 265 .backgroundColor(0xDCDCDC) 266 .padding({ top: 5 }) 267 } 268 } 269 ``` 270 271  272