• 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
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  ![en-us_image_0000001511900392](figures/en-us_image_0000001511900392.png)
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  ![en-us_image_0000001511580828](figures/en-us_image_0000001511580828.png)
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  ![en-us_image_0000001562940477](figures/en-us_image_0000001562940477.png)
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  ![en-us_image_0000001511740436](figures/en-us_image_0000001511740436.png)
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  import { router } from '@kit.ArkUI';
173
174  @Entry
175  @Component
176  struct ButtonCase1 {
177    @State FurL:router.RouterOptions = {'url':'pages/first_page'}
178    @State SurL:router.RouterOptions = {'url':'pages/second_page'}
179    @State TurL:router.RouterOptions = {'url':'pages/third_page'}
180    build() {
181      List({ space: 4 }) {
182        ListItem() {
183          Button("First").onClick(() => {
184            router.pushUrl(this.FurL)
185          })
186            .width('100%')
187        }
188        ListItem() {
189          Button("Second").onClick(() => {
190            router.pushUrl(this.SurL)
191          })
192            .width('100%')
193        }
194        ListItem() {
195          Button("Third").onClick(() => {
196            router.pushUrl(this.TurL)
197          })
198            .width('100%')
199        }
200      }
201      .listDirection(Axis.Vertical)
202      .backgroundColor(0xDCDCDC).padding(20)
203    }
204  }
205  ```
206
207  ![en-us_image_0000001562700393](figures/en-us_image_0000001562700393.png)
208
209
210- Using the button for submitting forms
211
212  On the user login/registration page, you can use a button to submit a login or registration request.
213
214  ```ts
215  // xxx.ets
216  @Entry
217  @Component
218  struct ButtonCase2 {
219    build() {
220      Column() {
221        TextInput({ placeholder: 'input your username' }).margin({ top: 20 })
222        TextInput({ placeholder: 'input your password' }).type(InputType.Password).margin({ top: 20 })
223        Button('Register').width(300).margin({ top: 20 })
224          .onClick(() => {
225            // Operation
226          })
227      }.padding(20)
228    }
229  }
230  ```
231
232  ![en-us_image_0000001562940473](figures/en-us_image_0000001562940473.png)
233
234- Configuring the button to float
235
236  The button can remain floating when the user swipes on the screen.
237
238  ```ts
239  // xxx.ets
240  @Entry
241  @Component
242  struct HoverButtonExample {
243    private arr: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
244    build() {
245      Stack() {
246        List({ space: 20, initialIndex: 0 }) {
247          ForEach(this.arr, (item:number) => {
248            ListItem() {
249              Text('' + item)
250                .width('100%').height(100).fontSize(16)
251                .textAlign(TextAlign.Center).borderRadius(10).backgroundColor(0xFFFFFF)
252            }
253          }, (item:number) => item.toString())
254        }.width('90%')
255        Button() {
256          Image($r('app.media.ic_public_add'))
257            .width(50)
258            .height(50)
259        }
260        .width(60)
261        .height(60)
262        .position({x: '80%', y: 600})
263        .shadow({radius: 10})
264        .onClick(() => {
265          // Operation
266        })
267      }
268      .width('100%')
269      .height('100%')
270      .backgroundColor(0xDCDCDC)
271      .padding({ top: 5 })
272    }
273  }
274  ```
275
276  ![floating_button](figures/floating_button.gif)
277