1e41f4b71Sopenharmony_ci# Button
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci
4e41f4b71Sopenharmony_ciThe **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).
5e41f4b71Sopenharmony_ci
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ci## Creating a Button
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ciYou can create a button that contains or does not contain child components.
10e41f4b71Sopenharmony_ci
11e41f4b71Sopenharmony_ci
12e41f4b71Sopenharmony_ci- Create a button that does not contain child components.
13e41f4b71Sopenharmony_ci
14e41f4b71Sopenharmony_ci  ```ts
15e41f4b71Sopenharmony_ci  Button(label?: ResourceStr, options?: { type?: ButtonType, stateEffect?: boolean })
16e41f4b71Sopenharmony_ci  ```
17e41f4b71Sopenharmony_ci
18e41f4b71Sopenharmony_ci  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.
19e41f4b71Sopenharmony_ci
20e41f4b71Sopenharmony_ci  ```ts
21e41f4b71Sopenharmony_ci  Button('Ok', { type: ButtonType.Normal, stateEffect: true }) 
22e41f4b71Sopenharmony_ci    .borderRadius(8) 
23e41f4b71Sopenharmony_ci    .backgroundColor(0x317aff) 
24e41f4b71Sopenharmony_ci    .width(90)
25e41f4b71Sopenharmony_ci    .height(40)
26e41f4b71Sopenharmony_ci  ```
27e41f4b71Sopenharmony_ci
28e41f4b71Sopenharmony_ci  ![en-us_image_0000001562820757](figures/en-us_image_0000001562820757.png)
29e41f4b71Sopenharmony_ci
30e41f4b71Sopenharmony_ci
31e41f4b71Sopenharmony_ci- Create a button that contains a single child component.
32e41f4b71Sopenharmony_ci
33e41f4b71Sopenharmony_ci  ```ts
34e41f4b71Sopenharmony_ci  Button(options?: {type?: ButtonType, stateEffect?: boolean})
35e41f4b71Sopenharmony_ci  ```
36e41f4b71Sopenharmony_ci
37e41f4b71Sopenharmony_ci  The child component contained can either be a basic component or a container component.
38e41f4b71Sopenharmony_ci
39e41f4b71Sopenharmony_ci  ```ts
40e41f4b71Sopenharmony_ci  Button({ type: ButtonType.Normal, stateEffect: true }) {
41e41f4b71Sopenharmony_ci    Row() {
42e41f4b71Sopenharmony_ci      Image($r('app.media.loading')).width(20).height(40).margin({ left: 12 })
43e41f4b71Sopenharmony_ci      Text('loading').fontSize(12).fontColor(0xffffff).margin({ left: 5, right: 12 })
44e41f4b71Sopenharmony_ci    }.alignItems(VerticalAlign.Center)
45e41f4b71Sopenharmony_ci  }.borderRadius(8).backgroundColor(0x317aff).width(90).height(40)
46e41f4b71Sopenharmony_ci  ```
47e41f4b71Sopenharmony_ci
48e41f4b71Sopenharmony_ci  ![en-us_image_0000001511421216](figures/en-us_image_0000001511421216.png)
49e41f4b71Sopenharmony_ci
50e41f4b71Sopenharmony_ci
51e41f4b71Sopenharmony_ci## Setting the Button Type
52e41f4b71Sopenharmony_ci
53e41f4b71Sopenharmony_ciUse the **type** parameter to set the button type to **Capsule**, **Circle**, or **Normal**.
54e41f4b71Sopenharmony_ci
55e41f4b71Sopenharmony_ci
56e41f4b71Sopenharmony_ci- Capsule button (default type)
57e41f4b71Sopenharmony_ci
58e41f4b71Sopenharmony_ci  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.
59e41f4b71Sopenharmony_ci
60e41f4b71Sopenharmony_ci  ```ts
61e41f4b71Sopenharmony_ci  Button('Disable', { type: ButtonType.Capsule, stateEffect: false }) 
62e41f4b71Sopenharmony_ci    .backgroundColor(0x317aff) 
63e41f4b71Sopenharmony_ci    .width(90)
64e41f4b71Sopenharmony_ci    .height(40)
65e41f4b71Sopenharmony_ci  ```
66e41f4b71Sopenharmony_ci
67e41f4b71Sopenharmony_ci  ![en-us_image_0000001511421208](figures/en-us_image_0000001511421208.png)
68e41f4b71Sopenharmony_ci
69e41f4b71Sopenharmony_ci
70e41f4b71Sopenharmony_ci- Circle button
71e41f4b71Sopenharmony_ci
72e41f4b71Sopenharmony_ci  Buttons of this type are round. The rounded corners cannot be reset through the **borderRadius** attribute.
73e41f4b71Sopenharmony_ci
74e41f4b71Sopenharmony_ci  ```ts
75e41f4b71Sopenharmony_ci  Button('Circle', { type: ButtonType.Circle, stateEffect: false }) 
76e41f4b71Sopenharmony_ci    .backgroundColor(0x317aff) 
77e41f4b71Sopenharmony_ci    .width(90) 
78e41f4b71Sopenharmony_ci    .height(90)
79e41f4b71Sopenharmony_ci  ```
80e41f4b71Sopenharmony_ci
81e41f4b71Sopenharmony_ci  ![en-us_image_0000001511740428](figures/en-us_image_0000001511740428.png)
82e41f4b71Sopenharmony_ci
83e41f4b71Sopenharmony_ci- Normal button
84e41f4b71Sopenharmony_ci
85e41f4b71Sopenharmony_ci  Buttons of this type have rounded corners set to 0. The rounded corners can be reset through the **borderRadius** attribute.
86e41f4b71Sopenharmony_ci
87e41f4b71Sopenharmony_ci  ```ts
88e41f4b71Sopenharmony_ci  Button('Ok', { type: ButtonType.Normal, stateEffect: true }) 
89e41f4b71Sopenharmony_ci    .borderRadius(8) 
90e41f4b71Sopenharmony_ci    .backgroundColor(0x317aff) 
91e41f4b71Sopenharmony_ci    .width(90)
92e41f4b71Sopenharmony_ci    .height(40)
93e41f4b71Sopenharmony_ci  ```
94e41f4b71Sopenharmony_ci
95e41f4b71Sopenharmony_ci  ![en-us_image_0000001563060641](figures/en-us_image_0000001563060641.png)
96e41f4b71Sopenharmony_ci
97e41f4b71Sopenharmony_ci
98e41f4b71Sopenharmony_ci## Setting Styles
99e41f4b71Sopenharmony_ci
100e41f4b71Sopenharmony_ci- Set the border radius.
101e41f4b71Sopenharmony_ci
102e41f4b71Sopenharmony_ci  You can use universal attributes to define the button styles. For example, you can use the **borderRadius** attribute to set the border radius.
103e41f4b71Sopenharmony_ci
104e41f4b71Sopenharmony_ci  ```ts
105e41f4b71Sopenharmony_ci  Button('circle border', { type: ButtonType.Normal }) 
106e41f4b71Sopenharmony_ci    .borderRadius(20)
107e41f4b71Sopenharmony_ci    .height(40)
108e41f4b71Sopenharmony_ci  ```
109e41f4b71Sopenharmony_ci
110e41f4b71Sopenharmony_ci  ![en-us_image_0000001511900392](figures/en-us_image_0000001511900392.png)
111e41f4b71Sopenharmony_ci
112e41f4b71Sopenharmony_ci
113e41f4b71Sopenharmony_ci- Set the text style.
114e41f4b71Sopenharmony_ci
115e41f4b71Sopenharmony_ci  Add text style attributes for the button.
116e41f4b71Sopenharmony_ci
117e41f4b71Sopenharmony_ci  ```ts
118e41f4b71Sopenharmony_ci  Button('font style', { type: ButtonType.Normal }) 
119e41f4b71Sopenharmony_ci    .fontSize(20) 
120e41f4b71Sopenharmony_ci    .fontColor(Color.Pink) 
121e41f4b71Sopenharmony_ci    .fontWeight(800)
122e41f4b71Sopenharmony_ci  ```
123e41f4b71Sopenharmony_ci
124e41f4b71Sopenharmony_ci  ![en-us_image_0000001511580828](figures/en-us_image_0000001511580828.png)
125e41f4b71Sopenharmony_ci
126e41f4b71Sopenharmony_ci
127e41f4b71Sopenharmony_ci- Set the background color.
128e41f4b71Sopenharmony_ci
129e41f4b71Sopenharmony_ci  Add the **backgroundColor** attribute for the button.
130e41f4b71Sopenharmony_ci
131e41f4b71Sopenharmony_ci  ```ts
132e41f4b71Sopenharmony_ci  Button('background color').backgroundColor(0xF55A42)
133e41f4b71Sopenharmony_ci  ```
134e41f4b71Sopenharmony_ci
135e41f4b71Sopenharmony_ci  ![en-us_image_0000001562940477](figures/en-us_image_0000001562940477.png)
136e41f4b71Sopenharmony_ci
137e41f4b71Sopenharmony_ci
138e41f4b71Sopenharmony_ci- Assign a function to the button.
139e41f4b71Sopenharmony_ci
140e41f4b71Sopenharmony_ci  In this example, the delete function is assigned to the button.
141e41f4b71Sopenharmony_ci
142e41f4b71Sopenharmony_ci  ```ts
143e41f4b71Sopenharmony_ci  let MarLeft: Record<string, number> = { 'left': 20 }
144e41f4b71Sopenharmony_ci  Button({ type: ButtonType.Circle, stateEffect: true }) {
145e41f4b71Sopenharmony_ci    Image($r('app.media.ic_public_delete_filled')).width(30).height(30)
146e41f4b71Sopenharmony_ci  }.width(55).height(55).margin(MarLeft).backgroundColor(0xF55A42)
147e41f4b71Sopenharmony_ci  ```
148e41f4b71Sopenharmony_ci
149e41f4b71Sopenharmony_ci  ![en-us_image_0000001511740436](figures/en-us_image_0000001511740436.png)
150e41f4b71Sopenharmony_ci
151e41f4b71Sopenharmony_ci
152e41f4b71Sopenharmony_ci## Adding Events
153e41f4b71Sopenharmony_ci
154e41f4b71Sopenharmony_ciThe **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.
155e41f4b71Sopenharmony_ci
156e41f4b71Sopenharmony_ci```ts
157e41f4b71Sopenharmony_ciButton('Ok', { type: ButtonType.Normal, stateEffect: true }) 
158e41f4b71Sopenharmony_ci  .onClick(()=>{ 
159e41f4b71Sopenharmony_ci    console.info('Button onClick') 
160e41f4b71Sopenharmony_ci  })
161e41f4b71Sopenharmony_ci```
162e41f4b71Sopenharmony_ci
163e41f4b71Sopenharmony_ci
164e41f4b71Sopenharmony_ci## Example
165e41f4b71Sopenharmony_ci
166e41f4b71Sopenharmony_ci- Using the button for startup
167e41f4b71Sopenharmony_ci
168e41f4b71Sopenharmony_ci  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.
169e41f4b71Sopenharmony_ci
170e41f4b71Sopenharmony_ci  ```ts
171e41f4b71Sopenharmony_ci  // xxx.ets
172e41f4b71Sopenharmony_ci  import { router } from '@kit.ArkUI';
173e41f4b71Sopenharmony_ci  
174e41f4b71Sopenharmony_ci  @Entry
175e41f4b71Sopenharmony_ci  @Component
176e41f4b71Sopenharmony_ci  struct ButtonCase1 {
177e41f4b71Sopenharmony_ci    @State FurL:router.RouterOptions = {'url':'pages/first_page'}
178e41f4b71Sopenharmony_ci    @State SurL:router.RouterOptions = {'url':'pages/second_page'}
179e41f4b71Sopenharmony_ci    @State TurL:router.RouterOptions = {'url':'pages/third_page'}
180e41f4b71Sopenharmony_ci    build() {
181e41f4b71Sopenharmony_ci      List({ space: 4 }) {
182e41f4b71Sopenharmony_ci        ListItem() {
183e41f4b71Sopenharmony_ci          Button("First").onClick(() => {
184e41f4b71Sopenharmony_ci            router.pushUrl(this.FurL)
185e41f4b71Sopenharmony_ci          })
186e41f4b71Sopenharmony_ci            .width('100%')
187e41f4b71Sopenharmony_ci        }
188e41f4b71Sopenharmony_ci        ListItem() {
189e41f4b71Sopenharmony_ci          Button("Second").onClick(() => {
190e41f4b71Sopenharmony_ci            router.pushUrl(this.SurL)
191e41f4b71Sopenharmony_ci          })
192e41f4b71Sopenharmony_ci            .width('100%')
193e41f4b71Sopenharmony_ci        }
194e41f4b71Sopenharmony_ci        ListItem() {
195e41f4b71Sopenharmony_ci          Button("Third").onClick(() => {
196e41f4b71Sopenharmony_ci            router.pushUrl(this.TurL)
197e41f4b71Sopenharmony_ci          })
198e41f4b71Sopenharmony_ci            .width('100%')
199e41f4b71Sopenharmony_ci        }
200e41f4b71Sopenharmony_ci      }
201e41f4b71Sopenharmony_ci      .listDirection(Axis.Vertical)
202e41f4b71Sopenharmony_ci      .backgroundColor(0xDCDCDC).padding(20)
203e41f4b71Sopenharmony_ci    }
204e41f4b71Sopenharmony_ci  }
205e41f4b71Sopenharmony_ci  ```
206e41f4b71Sopenharmony_ci
207e41f4b71Sopenharmony_ci  ![en-us_image_0000001562700393](figures/en-us_image_0000001562700393.png)
208e41f4b71Sopenharmony_ci
209e41f4b71Sopenharmony_ci
210e41f4b71Sopenharmony_ci- Using the button for submitting forms
211e41f4b71Sopenharmony_ci
212e41f4b71Sopenharmony_ci  On the user login/registration page, you can use a button to submit a login or registration request.
213e41f4b71Sopenharmony_ci
214e41f4b71Sopenharmony_ci  ```ts
215e41f4b71Sopenharmony_ci  // xxx.ets
216e41f4b71Sopenharmony_ci  @Entry
217e41f4b71Sopenharmony_ci  @Component
218e41f4b71Sopenharmony_ci  struct ButtonCase2 {
219e41f4b71Sopenharmony_ci    build() {
220e41f4b71Sopenharmony_ci      Column() {
221e41f4b71Sopenharmony_ci        TextInput({ placeholder: 'input your username' }).margin({ top: 20 })
222e41f4b71Sopenharmony_ci        TextInput({ placeholder: 'input your password' }).type(InputType.Password).margin({ top: 20 })
223e41f4b71Sopenharmony_ci        Button('Register').width(300).margin({ top: 20 })
224e41f4b71Sopenharmony_ci          .onClick(() => {
225e41f4b71Sopenharmony_ci            // Operation
226e41f4b71Sopenharmony_ci          })
227e41f4b71Sopenharmony_ci      }.padding(20)
228e41f4b71Sopenharmony_ci    }
229e41f4b71Sopenharmony_ci  }
230e41f4b71Sopenharmony_ci  ```
231e41f4b71Sopenharmony_ci
232e41f4b71Sopenharmony_ci  ![en-us_image_0000001562940473](figures/en-us_image_0000001562940473.png)
233e41f4b71Sopenharmony_ci
234e41f4b71Sopenharmony_ci- Configuring the button to float
235e41f4b71Sopenharmony_ci
236e41f4b71Sopenharmony_ci  The button can remain floating when the user swipes on the screen.
237e41f4b71Sopenharmony_ci
238e41f4b71Sopenharmony_ci  ```ts
239e41f4b71Sopenharmony_ci  // xxx.ets
240e41f4b71Sopenharmony_ci  @Entry
241e41f4b71Sopenharmony_ci  @Component
242e41f4b71Sopenharmony_ci  struct HoverButtonExample {
243e41f4b71Sopenharmony_ci    private arr: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
244e41f4b71Sopenharmony_ci    build() {
245e41f4b71Sopenharmony_ci      Stack() {
246e41f4b71Sopenharmony_ci        List({ space: 20, initialIndex: 0 }) {
247e41f4b71Sopenharmony_ci          ForEach(this.arr, (item:number) => {
248e41f4b71Sopenharmony_ci            ListItem() {
249e41f4b71Sopenharmony_ci              Text('' + item)
250e41f4b71Sopenharmony_ci                .width('100%').height(100).fontSize(16)
251e41f4b71Sopenharmony_ci                .textAlign(TextAlign.Center).borderRadius(10).backgroundColor(0xFFFFFF)
252e41f4b71Sopenharmony_ci            }
253e41f4b71Sopenharmony_ci          }, (item:number) => item.toString())
254e41f4b71Sopenharmony_ci        }.width('90%')
255e41f4b71Sopenharmony_ci        Button() {
256e41f4b71Sopenharmony_ci          Image($r('app.media.ic_public_add'))
257e41f4b71Sopenharmony_ci            .width(50)
258e41f4b71Sopenharmony_ci            .height(50)
259e41f4b71Sopenharmony_ci        }
260e41f4b71Sopenharmony_ci        .width(60)
261e41f4b71Sopenharmony_ci        .height(60)
262e41f4b71Sopenharmony_ci        .position({x: '80%', y: 600})
263e41f4b71Sopenharmony_ci        .shadow({radius: 10})
264e41f4b71Sopenharmony_ci        .onClick(() => {
265e41f4b71Sopenharmony_ci          // Operation
266e41f4b71Sopenharmony_ci        })
267e41f4b71Sopenharmony_ci      }
268e41f4b71Sopenharmony_ci      .width('100%')
269e41f4b71Sopenharmony_ci      .height('100%')
270e41f4b71Sopenharmony_ci      .backgroundColor(0xDCDCDC)
271e41f4b71Sopenharmony_ci      .padding({ top: 5 })
272e41f4b71Sopenharmony_ci    }
273e41f4b71Sopenharmony_ci  }
274e41f4b71Sopenharmony_ci  ```
275e41f4b71Sopenharmony_ci
276e41f4b71Sopenharmony_ci  ![floating_button](figures/floating_button.gif)
277