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

T
explain  
tianyu 已提交
3 4
按钮组件,可快速创建不同样式的按钮。

H
geshi  
HelloCrease 已提交
5
>  **说明:**
H
hebingxue 已提交
6
>
7
>  该组件从API Version 7开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
Z
zengyawen 已提交
8

Z
zengyawen 已提交
9

Z
zengyawen 已提交
10
## 子组件
Z
zengyawen 已提交
11

Z
zhongyuyan 已提交
12
可以包含单个子组件。
Z
zengyawen 已提交
13

Z
zengyawen 已提交
14 15 16

## 接口

H
hebingxue 已提交
17
**方法1:** Button(options?: {type?: ButtonType, stateEffect?: boolean})
18

H
hebingxue 已提交
19
**参数:**
Z
zengyawen 已提交
20

H
hebingxue 已提交
21 22
| 参数名         | 参数类型       | 必填        | 参数描述                              |
| ----------- | ---------- | ------| --------------------------------- |
23 24
| type        | ButtonType | 否    | 描述按钮显示样式。<br/>默认值:ButtonType.Capsule                           |
| stateEffect | boolean    | 否    |  按钮按下时是否开启按压态显示效果,当设置为false时,按压效果关闭。<br/>默认值:true |
Z
zengyawen 已提交
25

26

27
**方法2:** Button(label?: ResourceStr, options?: { type?: ButtonType, stateEffect?: boolean })
28

Z
zengyawen 已提交
29 30
  使用文本内容创建相应的按钮组件,此时Button无法包含子组件。

H
hebingxue 已提交
31
**参数:**
32

Y
yamila 已提交
33 34 35 36
| 参数名     | 参数类型                                | 必填   | 参数描述          |
| ------- | ----------------------------------- | ---- | ------------- |
| label   | [ResourceStr](ts-types.md#resourcestr) | 否    | 按钮文本内容。       |
| options | { type?: ButtonType, stateEffect?: boolean }   | 否    | 见方法1参数说明。 |
Z
zengyawen 已提交
37 38 39 40


## 属性

H
hebingxue 已提交
41 42 43
| 名称          | 参数类型           | 描述                                |
| ----------- | ----------- | --------------------------------- |
| type        | ButtonType  |  设置Button样式。<br/>默认值:ButtonType.Capsule                       |
44
| stateEffect | boolean     |  按钮按下时是否开启按压态显示效果,当设置为false时,按压效果关闭。<br/>默认值:true |
H
hebingxue 已提交
45 46 47 48 49 50 51

## ButtonType枚举说明
| 名称      | 描述                 |
| ------- | ------------------ |
| Capsule | 胶囊型按钮(圆角默认为高度的一半)。 |
| Circle  | 圆形按钮。              |
| Normal  | 普通按钮(默认不带圆角)。      |
Z
zengyawen 已提交
52

H
geshi  
HelloCrease 已提交
53
>  **说明:**
Y
yamila 已提交
54 55
>  - 按钮圆角通过[通用属性borderRadius](ts-universal-attributes-border.md)设置(不支持通过border接口设置圆角),且只支持设置参数为[Length](ts-types.md#length)的圆角。
>  - 当按钮类型为Capsule时,borderRadius设置不生效,按钮圆角始终为宽、高中较小值的一半。
56 57
>  - 当按钮类型为Circle时,borderRadius即为按钮半径,若未设置borderRadius按钮半径则为宽、高中较小值的一半。
>  - 按钮文本通过[通用文本样式](ts-universal-attributes-text-style.md)进行设置。
58
>  - 设置[颜色渐变](ts-universal-attributes-gradient-color.md)需先设置[backgroundColor](ts-universal-attributes-background.md)为透明色。
Z
zengyawen 已提交
59 60 61


## 示例
Z
zengyawen 已提交
62

H
geshi  
HelloCrease 已提交
63 64
```ts
// xxx.ets
Z
zengyawen 已提交
65 66 67 68 69
@Entry
@Component
struct ButtonExample {
  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Start, justifyContent: FlexAlign.SpaceBetween }) {
70
      Text('Normal button').fontSize(9).fontColor(0xCCCCCC)
Z
zengyawen 已提交
71
      Flex({ alignItems: ItemAlign.Center, justifyContent: FlexAlign.SpaceBetween }) {
72
        Button('OK', { type: ButtonType.Normal, stateEffect: true }).borderRadius(8).backgroundColor(0x317aff).width(90)
Z
zengyawen 已提交
73 74
        Button({ type: ButtonType.Normal, stateEffect: true }) {
          Row() {
75
            LoadingProgress().width(20).height(20).margin({ left: 12 }).color(0xFFFFFF)
Z
zengyawen 已提交
76 77
            Text('loading').fontSize(12).fontColor(0xffffff).margin({ left: 5, right: 12 })
          }.alignItems(VerticalAlign.Center)
78 79 80
        }.borderRadius(8).backgroundColor(0x317aff).width(90).height(40)

        Button('Disable', { type: ButtonType.Normal, stateEffect: false }).opacity(0.4)
Z
zengyawen 已提交
81 82 83 84 85
          .borderRadius(8).backgroundColor(0x317aff).width(90)
      }

      Text('Capsule button').fontSize(9).fontColor(0xCCCCCC)
      Flex({ alignItems: ItemAlign.Center, justifyContent: FlexAlign.SpaceBetween }) {
86
        Button('OK', { type: ButtonType.Capsule, stateEffect: true }).backgroundColor(0x317aff).width(90)
Z
zengyawen 已提交
87 88
        Button({ type: ButtonType.Capsule, stateEffect: true }) {
          Row() {
89
            LoadingProgress().width(20).height(20).margin({ left: 12 }).color(0xFFFFFF)
Z
zengyawen 已提交
90
            Text('loading').fontSize(12).fontColor(0xffffff).margin({ left: 5, right: 12 })
91
          }.alignItems(VerticalAlign.Center).width(90).height(40)
Z
zengyawen 已提交
92
        }.backgroundColor(0x317aff)
93 94

        Button('Disable', { type: ButtonType.Capsule, stateEffect: false }).opacity(0.4)
Z
zengyawen 已提交
95 96 97 98 99 100
          .backgroundColor(0x317aff).width(90)
      }

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

Z
zengyawen 已提交
104
        Button({ type: ButtonType.Circle, stateEffect: true }) {
105
          LoadingProgress().width(20).height(20).color(0xFFFFFF)
Z
zengyawen 已提交
106 107 108 109 110 111 112
        }.width(55).height(55).margin({ left: 20 }).backgroundColor(0xF55A42)
      }
    }.height(400).padding({ left: 35, right: 35, top: 35 })
  }
}
```

Y
yamila 已提交
113
![button](figures/button.gif)