ts-basic-components-button.md 5.0 KB
Newer Older
Z
zengyawen 已提交
1
# Button
Z
zengyawen 已提交
2

3
The **\<Button>** component can be used to create different types of buttons.
Z
zengyawen 已提交
4

5 6
> **NOTE**
>
Z
zengyawen 已提交
7
> This component is supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version.
Z
zengyawen 已提交
8

Z
zengyawen 已提交
9 10

## Required Permissions
Z
zengyawen 已提交
11 12 13

None

Z
zengyawen 已提交
14

E
esterzhou 已提交
15
## Child Components
Z
zengyawen 已提交
16

E
esterzhou 已提交
17
Supported
Z
zengyawen 已提交
18 19 20 21 22


## APIs

- Button(options?: {type?: ButtonType, stateEffect?: boolean})
E
ester.zhou 已提交
23 24
  
  **Table 1** options parameters
Z
zengyawen 已提交
25
  
E
esterzhou 已提交
26
  | Name | Type | Mandatory | Default Value | Description |
Z
zengyawen 已提交
27
  | -------- | -------- | -------- | -------- | -------- |
E
esterzhou 已提交
28 29
  | type | ButtonType | No | Capsule | Button type. |
  | stateEffect | boolean | No | true | Whether to enable the state switchover effect when a button is pressed. When the state is set to **false**, the state switchover effect is disabled. |
Z
zengyawen 已提交
30 31


32
- Button(label?: ResourceStr, options?: { type?: ButtonType, stateEffect?: boolean })
E
ester.zhou 已提交
33
  
34
  Creates a button component based on text content. In this case, the **\<Button>** component cannot contain child components.
Z
zengyawen 已提交
35

36
  **Table 2** Parameters
E
ester.zhou 已提交
37

38 39
  | Name | Type | Mandatory | Default Value | Description |
  | -------- | -------- | -------- | -------- | -------- |
40
  | label | [ResourceStr](../../ui/ts-types.md) | No | - | Button text. |
41
  | options | Object | No | - | For details, see the **options** parameters. |
Z
zengyawen 已提交
42 43 44 45


## Attributes

E
esterzhou 已提交
46
| Name | Type | Default Value | Description |
Z
zengyawen 已提交
47
| -------- | -------- | -------- | -------- |
E
esterzhou 已提交
48 49
| type | ButtonType | Capsule | Button type. |
| stateEffect | boolean | true | Whether to enable the state switchover effect. When the state is set to **false**, the state switchover effect is disabled. |
Z
zengyawen 已提交
50

51

52
**ButtonType** enums
53 54 55
| Name | Description |
| -------- | -------- |
| Capsule | Capsule-type button (the round corner is half of the height by default). |
56 57
| Circle  | Circle button. |
| Normal  | Normal button (without rounded corners by default). |
Z
zengyawen 已提交
58

59
> **NOTE**
Z
zengyawen 已提交
60
> - The rounded corner of a button is set by using [Border](ts-universal-attributes-border.md). (The rounded corner cannot be set by using a border API.)
E
esterzhou 已提交
61 62
> - When the button type is **Capsule**, the **borderRadius** settings do not take effect, and the rounded corner of the button is always half of the button height. 
> - When the button type is **Circle**, the value of **borderRadius** is used as the button radius. If **borderRadius** is not set, the button radius is half of the width or height, whichever is smaller. 
Z
zengyawen 已提交
63 64 65 66 67
> - The button text is set using the [common text style](ts-universal-attributes-text-style.md).


## Example

Z
zengyawen 已提交
68

69 70
```ts
// xxx.ets
Z
zengyawen 已提交
71 72 73 74 75 76 77 78 79 80
@Entry
@Component
struct ButtonExample {
  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Start, justifyContent: FlexAlign.SpaceBetween }) {
      Text('Common button').fontSize(9).fontColor(0xCCCCCC)
      Flex({ alignItems: ItemAlign.Center, justifyContent: FlexAlign.SpaceBetween }) {
        Button('Ok', { type: ButtonType.Normal, stateEffect: true }).borderRadius(8).backgroundColor(0x317aff).width(90)
        Button({ type: ButtonType.Normal, stateEffect: true }) {
          Row() {
81
            // You are advised to reference an image by using $r to create a Resource object.
Z
zengyawen 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
            Image($r('app.media.loading')).width(20).height(20).margin({ left: 12 })
            Text('loading').fontSize(12).fontColor(0xffffff).margin({ left: 5, right: 12 })
          }.alignItems(VerticalAlign.Center)
        }.borderRadius(8).backgroundColor(0x317aff).width(90)
        Button('Disable', { type: ButtonType.Normal, stateEffect: false }).opacity(0.5)
          .borderRadius(8).backgroundColor(0x317aff).width(90)
      }

      Text('Capsule button').fontSize(9).fontColor(0xCCCCCC)
      Flex({ alignItems: ItemAlign.Center, justifyContent: FlexAlign.SpaceBetween }) {
        Button('Ok', { type: ButtonType.Capsule, stateEffect: true }).backgroundColor(0x317aff).width(90)
        Button({ type: ButtonType.Capsule, stateEffect: true }) {
          Row() {
            Image($r('app.media.loading')).width(20).height(20).margin({ left: 12 })
            Text('loading').fontSize(12).fontColor(0xffffff).margin({ left: 5, right: 12 })
          }.alignItems(VerticalAlign.Center).width(90)
        }.backgroundColor(0x317aff)
        .onClick((event: ClickEvent) => {
          AlertDialog.show({ message: 'The login is successful' })
        })
        Button('Disable', { type: ButtonType.Capsule, stateEffect: false }).opacity(0.5)
          .backgroundColor(0x317aff).width(90)
      }

      Text('Circle button').fontSize(9).fontColor(0xCCCCCC)
      Flex({ alignItems: ItemAlign.Center, wrap: FlexWrap.Wrap }) {
        Button({ type: ButtonType.Circle, stateEffect: true }) {
          Image($r('app.media.ic_public_app_filled')).width(20).height(20)
        }.width(55).height(55).backgroundColor(0x317aff)
        Button({ type: ButtonType.Circle, stateEffect: true }) {
          Image($r('app.media.ic_public_delete_filled')).width(30).height(30)
        }.width(55).height(55).margin({ left: 20 }).backgroundColor(0xF55A42)
      }
    }.height(400).padding({ left: 35, right: 35, top: 35 })
  }
}
```

Z
zengyawen 已提交
120
![en-us_image_0000001257138341](figures/en-us_image_0000001257138341.gif)