ts-basic-components-button.md 7.5 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

E
ester.zhou 已提交
5
>  **NOTE**
6
>
E
ester.zhou 已提交
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

E
ester.zhou 已提交
10
## Child Components
Z
zengyawen 已提交
11

E
ester.zhou 已提交
12
This component can contain only one child component.
Z
zengyawen 已提交
13

Z
zengyawen 已提交
14

E
ester.zhou 已提交
15
## APIs
Z
zengyawen 已提交
16

E
ester.zhou 已提交
17
**API 1:** Button(options?: {type?: ButtonType, stateEffect?: boolean})
Z
zengyawen 已提交
18

E
ester.zhou 已提交
19 20
Since API version 9, this API is supported in ArkTS widgets.

E
ester.zhou 已提交
21
**Parameters**
Z
zengyawen 已提交
22

E
ester.zhou 已提交
23 24 25
| Name        | Type      | Mandatory       | Description                             |
| ----------- | ---------- | ------| --------------------------------- |
| type        | ButtonType | No   | Button type.<br>Default value: **ButtonType.Capsule**                          |
E
ester.zhou 已提交
26
| stateEffect | boolean    | No   |  Whether to enable the pressed effect on the click of the button. The value **false** means to disable the pressed effect.<br>Default value: **true**|
Z
zengyawen 已提交
27

E
ester.zhou 已提交
28
**API 2:** Button(label?: ResourceStr, options?: { type?: ButtonType, stateEffect?: boolean })
Z
zengyawen 已提交
29

E
ester.zhou 已提交
30
Creates a button component based on text content. In this case, the component cannot contain child components.
Z
zengyawen 已提交
31

E
ester.zhou 已提交
32 33
Since API version 9, this API is supported in ArkTS widgets.

E
ester.zhou 已提交
34
**Parameters**
E
ester.zhou 已提交
35

E
ester.zhou 已提交
36 37 38 39
| Name    | Type                               | Mandatory  | Description         |
| ------- | ----------------------------------- | ---- | ------------- |
| label   | [ResourceStr](ts-types.md#resourcestr) | No   | Button text.      |
| options | { type?: ButtonType, stateEffect?: boolean }   | No   | See parameters of API 1.|
Z
zengyawen 已提交
40 41 42 43


## Attributes

E
ester.zhou 已提交
44 45
| Name         | Type          | Description                               |
| ----------- | ----------- | --------------------------------- |
E
ester.zhou 已提交
46 47
| type        | ButtonType  | Button type.<br>Default value: **ButtonType.Capsule**<br>Since API version 9, this API is supported in ArkTS widgets.|
| stateEffect | boolean     | Whether to enable the pressed effect on the click of the button. The value **false** means to disable the pressed effect.<br>Default value: **true**<br>Since API version 9, this API is supported in ArkTS widgets.|
E
ester.zhou 已提交
48
| labelStyle<sup>10+</sup> | [LabelStyle](#labelstyle10) | Label style of the button.|
49

E
ester.zhou 已提交
50
## ButtonType
E
ester.zhou 已提交
51 52 53

Since API version 9, this API is supported in ArkTS widgets.

E
ester.zhou 已提交
54 55 56 57 58
| Name     | Description                |
| ------- | ------------------ |
| Capsule | Capsule-type button (the round corner is half of the height by default).|
| Circle  | Circle button.             |
| Normal  | Normal button (without rounded corners by default).     |
Z
zengyawen 已提交
59

E
ester.zhou 已提交
60
>  **NOTE**
E
ester.zhou 已提交
61 62
>  - The rounded corner of a button is set by using [borderRadius](ts-universal-attributes-border.md), rather than by using the **border** API. Only a rounded corner whose parameter is [Length](ts-types.md#length) is supported.
>  - For a button of the **Capsule** type, the **borderRadius** settings do not take effect, and the radius of its rounded corner is always half of the button height or width, whichever is smaller.
E
ester.zhou 已提交
63 64
>  - For a button of the **Circle** type, its radius is the value of **borderRadius** (if set) or the width or height (whichever is smaller).
>  - The button text is set using the [text style attributes](ts-universal-attributes-text-style.md).
E
ester.zhou 已提交
65 66 67 68 69 70 71 72 73 74 75 76
>  - Before setting the [gradient color](ts-universal-attributes-gradient-color.md), you need to set [backgroundColor](ts-universal-attributes-background.md) to transparent.

## LabelStyle<sup>10+</sup>

| Name                | Type                                                    | Mandatory| Description                                                        |
| -------------------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
| overflow             | [TextOverflow](ts-appendix-enums.md#textoverflow)            | No  | Display mode when the label text is too long. Text is clipped at the transition between words. To clip text in the middle of a word, add **\u200B** between characters.|
| maxLines             | number                                                       | No  | Maximum number of lines in the label text. By default, text is automatically folded. If this attribute is specified, the text will not exceed the specified number of lines. If there is extra text, you can use **textOverflow** to specify how it is displayed.|
| minFontSize          | number \| [ResourceStr](ts-types.md#resourcestr)             | No  | Minimum font size of the label text. For the setting to take effect, this attribute must be used together with **maxFontSize**, **maxLines**, or layout constraint settings.|
| maxFontSize          | number \| [ResourceStr](ts-types.md#resourcestr)             | No  | Maximum font size of the label text. For the setting to take effect, this attribute must be used together with **minFontSize**, **maxLines**, or layout constraint settings.|
| heightAdaptivePolicy | [TextHeightAdaptivePolicy](ts-appendix-enums.md#textheightadaptivepolicy10) | No  | How the adaptive height is determined for the label text.                             |
| font                 | [Font](ts-types.md#Font)                                     | No  | Font of the label text.                                     |
Z
zengyawen 已提交
77 78 79

## Example

80 81
```ts
// xxx.ets
Z
zengyawen 已提交
82 83 84 85 86
@Entry
@Component
struct ButtonExample {
  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Start, justifyContent: FlexAlign.SpaceBetween }) {
E
ester.zhou 已提交
87
      Text('Normal button').fontSize(9).fontColor(0xCCCCCC)
Z
zengyawen 已提交
88
      Flex({ alignItems: ItemAlign.Center, justifyContent: FlexAlign.SpaceBetween }) {
E
ester.zhou 已提交
89
        Button('OK', { type: ButtonType.Normal, stateEffect: true }).borderRadius(8).backgroundColor(0x317aff).width(90)
Z
zengyawen 已提交
90 91
        Button({ type: ButtonType.Normal, stateEffect: true }) {
          Row() {
E
ester.zhou 已提交
92
            LoadingProgress().width(20).height(20).margin({ left: 12 }).color(0xFFFFFF)
Z
zengyawen 已提交
93 94
            Text('loading').fontSize(12).fontColor(0xffffff).margin({ left: 5, right: 12 })
          }.alignItems(VerticalAlign.Center)
E
ester.zhou 已提交
95 96 97
        }.borderRadius(8).backgroundColor(0x317aff).width(90).height(40)

        Button('Disable', { type: ButtonType.Normal, stateEffect: false }).opacity(0.4)
Z
zengyawen 已提交
98 99 100 101 102
          .borderRadius(8).backgroundColor(0x317aff).width(90)
      }

      Text('Capsule button').fontSize(9).fontColor(0xCCCCCC)
      Flex({ alignItems: ItemAlign.Center, justifyContent: FlexAlign.SpaceBetween }) {
E
ester.zhou 已提交
103
        Button('OK', { type: ButtonType.Capsule, stateEffect: true }).backgroundColor(0x317aff).width(90)
Z
zengyawen 已提交
104 105
        Button({ type: ButtonType.Capsule, stateEffect: true }) {
          Row() {
E
ester.zhou 已提交
106
            LoadingProgress().width(20).height(20).margin({ left: 12 }).color(0xFFFFFF)
Z
zengyawen 已提交
107
            Text('loading').fontSize(12).fontColor(0xffffff).margin({ left: 5, right: 12 })
E
ester.zhou 已提交
108
          }.alignItems(VerticalAlign.Center).width(90).height(40)
Z
zengyawen 已提交
109
        }.backgroundColor(0x317aff)
E
ester.zhou 已提交
110 111

        Button('Disable', { type: ButtonType.Capsule, stateEffect: false }).opacity(0.4)
Z
zengyawen 已提交
112 113 114 115 116 117
          .backgroundColor(0x317aff).width(90)
      }

      Text('Circle button').fontSize(9).fontColor(0xCCCCCC)
      Flex({ alignItems: ItemAlign.Center, wrap: FlexWrap.Wrap }) {
        Button({ type: ButtonType.Circle, stateEffect: true }) {
E
ester.zhou 已提交
118
          LoadingProgress().width(20).height(20).color(0xFFFFFF)
Z
zengyawen 已提交
119
        }.width(55).height(55).backgroundColor(0x317aff)
E
ester.zhou 已提交
120

Z
zengyawen 已提交
121
        Button({ type: ButtonType.Circle, stateEffect: true }) {
E
ester.zhou 已提交
122
          LoadingProgress().width(20).height(20).color(0xFFFFFF)
Z
zengyawen 已提交
123 124 125 126 127 128 129
        }.width(55).height(55).margin({ left: 20 }).backgroundColor(0xF55A42)
      }
    }.height(400).padding({ left: 35, right: 35, top: 35 })
  }
}
```

E
ester.zhou 已提交
130
![button](figures/button.gif)