• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  ![en-us_image_0000001562820757](figures/en-us_image_0000001562820757.png)
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  ![en-us_image_0000001511421216](figures/en-us_image_0000001511421216.png)
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  ![en-us_image_0000001511421208](figures/en-us_image_0000001511421208.png)
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  ![en-us_image_0000001511740428](figures/en-us_image_0000001511740428.png)
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  ![en-us_image_0000001563060641](figures/en-us_image_0000001563060641.png)
96
97- Rounded rectangle button
98  The rounded rectangle button has a default corner radius of 20 vp when [controlSize](../reference/apis-arkui/arkui-ts/ts-basic-components-button.md#controlsize11) is **NORMAL**, and 14 vp when [controlSize](../reference/apis-arkui/arkui-ts/ts-basic-components-button.md#controlsize11) is **SMALL**. You can define custom corner radius settings through **borderRadius**.
99
100  ```ts
101  Button('Disable', { type: ButtonType.ROUNDED_RECTANGLE, stateEffect: true })
102    .backgroundColor(0x317aff)
103    .width(90)
104    .height(40)
105  ```
106
107  ![en-us_image_0000001563060641](figures/en-us_image_0000001511421208.png)
108
109## Setting Styles
110
111- Setting the border radius
112
113  You can use universal attributes to define the button styles. For example, you can use the **borderRadius** attribute to set the border radius.
114
115  ```ts
116  Button('circle border', { type: ButtonType.Normal })
117    .borderRadius(20)
118    .height(40)
119  ```
120
121  ![en-us_image_0000001511900392](figures/en-us_image_0000001511900392.png)
122
123
124- Setting the text style
125
126  Add text style attributes for the button.
127
128  ```ts
129  Button('font style', { type: ButtonType.Normal })
130    .fontSize(20)
131    .fontColor(Color.Pink)
132    .fontWeight(800)
133  ```
134
135  ![en-us_image_0000001511580828](figures/en-us_image_0000001511580828.png)
136
137
138- Setting the background color
139
140  Add the **backgroundColor** attribute for the button.
141
142  ```ts
143  Button('background color').backgroundColor(0xF55A42)
144  ```
145
146  ![en-us_image_0000001562940477](figures/en-us_image_0000001562940477.png)
147
148
149- Assigning a function to the button
150
151  In this example, the delete function is assigned to the button.
152
153  ```ts
154  let MarLeft: Record<string, number> = { 'left': 20 }
155  Button({ type: ButtonType.Circle, stateEffect: true }) {
156    Image($r('app.media.ic_public_delete_filled')).width(30).height(30)
157  }.width(55).height(55).margin(MarLeft).backgroundColor(0xF55A42)
158  ```
159
160  ![en-us_image_0000001511740436](figures/en-us_image_0000001511740436.png)
161
162
163## Adding Events
164
165The **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.
166
167```ts
168Button('Ok', { type: ButtonType.Normal, stateEffect: true })
169  .onClick(()=>{
170    console.info('Button onClick')
171  })
172```
173
174
175## Example
176
177- Using the button for startup
178
179  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.
180
181  ```ts
182  // xxx.ets
183  @Entry
184  @Component
185  struct ButtonCase1 {
186    pathStack: NavPathStack = new NavPathStack();
187
188    @Builder
189    PageMap(name: string) {
190      if (name === "first_page") {
191        pageOneTmp()
192      } else if (name === "second_page") {
193        pageTwoTmp()
194      } else if (name === "third_page") {
195        pageThreeTmp()
196      }
197    }
198
199    build() {
200      Navigation(this.pathStack) {
201        List({ space: 4 }) {
202          ListItem() {
203            Button("First").onClick(() => {
204              this.pathStack.pushPath({ name: "first_page"})
205            })
206              .width('100%')
207          }
208
209          ListItem() {
210            Button("Second").onClick(() => {
211              this.pathStack.pushPath({ name: "second_page"})
212            })
213              .width('100%')
214          }
215
216          ListItem() {
217            Button("Third").onClick(() => {
218              this.pathStack.pushPath({ name: "third_page"})
219            })
220              .width('100%')
221          }
222        }
223        .listDirection(Axis.Vertical)
224        .backgroundColor(0xDCDCDC).padding(20)
225      }
226      .mode(NavigationMode.Stack)
227      .navDestination(this.PageMap)
228    }
229  }
230
231  // pageOne
232  @Component
233  export struct pageOneTmp {
234    pathStack: NavPathStack = new NavPathStack();
235
236    build() {
237      NavDestination() {
238        Column() {
239          Text("first_page")
240        }.width('100%').height('100%')
241      }.title("pageOne")
242      .onBackPressed(() => {
243        const popDestinationInfo = this.pathStack.pop() // Pop the top element out of the navigation stack.
244        console.log('pop' + 'Return value' + JSON.stringify(popDestinationInfo))
245        return true
246      })
247      .onReady((context: NavDestinationContext) => {
248        this.pathStack = context.pathStack
249      })
250    }
251  }
252
253  // pageTwo
254  @Component
255  export struct pageTwoTmp {
256    pathStack: NavPathStack = new NavPathStack();
257
258    build() {
259      NavDestination() {
260        Column() {
261          Text("second_page")
262        }.width('100%').height('100%')
263      }.title("pageTwo")
264      .onBackPressed(() => {
265        const popDestinationInfo = this.pathStack.pop() // Pop the top element out of the navigation stack.
266        console.log('pop' + 'Return value' + JSON.stringify(popDestinationInfo))
267        return true
268      })
269      .onReady((context: NavDestinationContext) => {
270        this.pathStack = context.pathStack
271      })
272    }
273  }
274
275  // pageThree
276  @Component
277  export struct pageThreeTmp {
278    pathStack: NavPathStack = new NavPathStack();
279
280    build() {
281      NavDestination() {
282        Column() {
283          Text("third_page")
284        }.width('100%').height('100%')
285      }.title("pageThree")
286      .onBackPressed(() => {
287        const popDestinationInfo = this.pathStack.pop() // Pop the top element out of the navigation stack.
288        console.log('pop' + 'Return value' + JSON.stringify(popDestinationInfo))
289        return true
290      })
291      .onReady((context: NavDestinationContext) => {
292        this.pathStack = context.pathStack
293      })
294    }
295  }
296  ```
297
298  ![en-us_image_0000001562700393](figures/en-us_image_0000001562940814.gif)
299
300
301- Using the button for submitting forms
302
303  On the user login/registration page, you can use a button to submit a login or registration request.
304
305  ```ts
306  // xxx.ets
307  @Entry
308  @Component
309  struct ButtonCase2 {
310    build() {
311      Column() {
312        TextInput({ placeholder: 'input your username' }).margin({ top: 20 })
313        TextInput({ placeholder: 'input your password' }).type(InputType.Password).margin({ top: 20 })
314        Button('Register').width(300).margin({ top: 20 })
315          .onClick(() => {
316            // Operation
317          })
318      }.padding(20)
319    }
320  }
321  ```
322
323  ![en-us_image_0000001562940473](figures/en-us_image_0000001562940473.png)
324
325- Configuring the button to float
326
327  The button can remain floating when the user swipes on the screen.
328
329  ```ts
330  // xxx.ets
331  @Entry
332  @Component
333  struct HoverButtonExample {
334    private arr: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
335    build() {
336      Stack() {
337        List({ space: 20, initialIndex: 0 }) {
338          ForEach(this.arr, (item:number) => {
339            ListItem() {
340              Text('' + item)
341                .width('100%').height(100).fontSize(16)
342                .textAlign(TextAlign.Center).borderRadius(10).backgroundColor(0xFFFFFF)
343            }
344          }, (item:number) => item.toString())
345        }.width('90%')
346        Button() {
347          Image($r('app.media.ic_public_add'))
348            .width(50)
349            .height(50)
350        }
351        .width(60)
352        .height(60)
353        .position({x: '80%', y: 600})
354        .shadow({radius: 10})
355        .onClick(() => {
356          // Operation
357        })
358      }
359      .width('100%')
360      .height('100%')
361      .backgroundColor(0xDCDCDC)
362      .padding({ top: 5 })
363    }
364  }
365  ```
366
367  ![floating_button](figures/floating_button.gif)
368