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