• 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/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  ![en-us_image_0000001562820757](figures/en-us_image_0000001562820757.png)
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  ![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  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.
58
59  ```ts
60  Button('Disable', { type: ButtonType.Capsule, stateEffect: false })
61    .backgroundColor(0x317aff)
62    .width(90)
63    .height(40)
64  ```
65
66  ![en-us_image_0000001511421208](figures/en-us_image_0000001511421208.png)
67
68
69- Circle button
70  Buttons of this type are round. The rounded corners cannot be reset through the **borderRadius** attribute.
71
72  ```ts
73  Button('Circle', { type: ButtonType.Circle, stateEffect: false })
74    .backgroundColor(0x317aff)
75    .width(90)
76    .height(90)
77  ```
78
79  ![en-us_image_0000001511740428](figures/en-us_image_0000001511740428.png)
80
81- Normal button
82  Buttons of this type have rounded corners set to 0. The rounded corners can be reset through the **borderRadius** attribute.
83
84  ```ts
85  Button('Ok', { type: ButtonType.Normal, stateEffect: true })
86    .borderRadius(8)
87    .backgroundColor(0x317aff)
88    .width(90)
89    .height(40)
90  ```
91
92  ![en-us_image_0000001563060641](figures/en-us_image_0000001563060641.png)
93
94
95## Setting Styles
96
97- Set the border radius:
98  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.
99
100  ```ts
101  Button('circle border', { type: ButtonType.Normal })
102    .borderRadius(20)
103    .height(40)
104  ```
105
106  ![en-us_image_0000001511900392](figures/en-us_image_0000001511900392.png)
107
108
109- The **Font** type is used to set the text style.
110  Add a font style for text displayed on the button.
111
112  ```ts
113  Button('font style', { type: ButtonType.Normal })
114    .fontSize(20)
115    .fontColor(Color.Pink)
116    .fontWeight(800)
117  ```
118
119  ![en-us_image_0000001511580828](figures/en-us_image_0000001511580828.png)
120
121
122- Set the background color:
123  You can do so by adding the **backgroundColor** attribute.
124
125  ```ts
126  Button('background color').backgroundColor(0xF55A42)
127  ```
128
129  ![en-us_image_0000001562940477](figures/en-us_image_0000001562940477.png)
130
131
132- Assign a function to the button:
133  In this example, the delete function is assigned to the button.
134
135  ```ts
136  Button({ type: ButtonType.Circle, stateEffect: true }) {
137    Image($r('app.media.ic_public_delete_filled')).width(30).height(30)
138  }.width(55).height(55).margin({ left: 20 }).backgroundColor(0xF55A42)
139  ```
140
141  ![en-us_image_0000001511740436](figures/en-us_image_0000001511740436.png)
142
143
144## Adding Events
145
146The **\<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.
147
148```ts
149Button('Ok', { type: ButtonType.Normal, stateEffect: true })
150  .onClick(()=>{
151    console.info('Button onClick')
152  })
153```
154
155
156## Example Scenario
157
158- Using the Button for Startup
159  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.
160
161  ```ts
162  // xxx.ets
163  import router from '@ohos.router';
164  @Entry
165  @Component
166  struct ButtonCase1 {
167    build() {
168      List({ space: 4 }) {
169        ListItem() {
170          Button("First").onClick(() => {
171            router.pushUrl({ url: 'pages/first_page' })
172          })
173            .width('100%')
174        }
175        ListItem() {
176          Button("Second").onClick(() => {
177            router.pushUrl({ url: 'pages/second_page' })
178          })
179            .width('100%')
180        }
181        ListItem() {
182          Button("Third").onClick(() => {
183            router.pushUrl({ url: 'pages/third_page' })
184          })
185            .width('100%')
186        }
187      }
188      .listDirection(Axis.Vertical)
189      .backgroundColor(0xDCDCDC).padding(20)
190    }
191  }
192  ```
193
194  ![en-us_image_0000001562700393](figures/en-us_image_0000001562700393.png)
195
196
197- Use the button for submitting forms:
198  On the user login/registration page, you can use a button to submit a login or registration request.
199
200  ```ts
201  // xxx.ets
202  @Entry
203  @Component
204  struct ButtonCase2 {
205    build() {
206      Column() {
207        TextInput({ placeholder: 'input your username' }).margin({ top: 20 })
208        TextInput({ placeholder: 'input your password' }).type(InputType.Password).margin({ top: 20 })
209        Button('Register').width(300).margin({ top: 20 })
210          .onClick(() => {
211            // Operation
212          })
213      }.padding(20)
214    }
215  }
216  ```
217
218  ![en-us_image_0000001562940473](figures/en-us_image_0000001562940473.png)
219
220- Configure the button to float:
221  The button can remain floating when the user swipes on the screen.
222
223  ```ts
224  // xxx.ets
225  @Entry
226  @Component
227  struct HoverButtonExample {
228    private arr: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
229    build() {
230      Stack() {
231        List({ space: 20, initialIndex: 0 }) {
232          ForEach(this.arr, (item) => {
233            ListItem() {
234              Text('' + item)
235                .width('100%').height(100).fontSize(16)
236                .textAlign(TextAlign.Center).borderRadius(10).backgroundColor(0xFFFFFF)
237            }
238          }, item => item)
239        }.width('90%')
240        Button() {
241          Image($r('app.media.ic_public_add'))
242            .width(50)
243            .height(50)
244        }
245        .width(60)
246        .height(60)
247        .position({x: '80%', y: 600})
248        .shadow({radius: 10})
249        .onClick(() => {
250          // Operation
251        })
252      }
253      .width('100%')
254      .height('100%')
255      .backgroundColor(0xDCDCDC)
256      .padding({ top: 5 })
257    }
258  }
259  ```
260
261  ![GIF](figures/GIF.gif)
262