文档管理中心

您当前正在浏览HarmonyOS开发者3.0版本配套的开发者文档,对应API能力级别为API 8 Release。此版本文档已归档不再维护,推荐您使用HarmonyOS NEXT版本

按钮组件,可快速创建不同样式的按钮。

说明

该组件从API Version 7开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。

子组件

可以包含单个子组件。

接口

方法1: Button(options?: {type?: ButtonType, stateEffect?: boolean})

参数:

展开

参数名

参数类型

必填

参数描述

type

ButtonType

描述按钮显示样式。

默认值:ButtonType.Capsule

stateEffect

boolean

按钮按下时是否开启按压态显示效果,当设置为false时,按压效果关闭。。

默认值:true

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

使用文本内容创建相应的按钮组件,此时Button无法包含子组件。

参数:

展开

参数名

参数类型

必填

参数描述

label

ResourceStr

按钮文本内容。

options

{

type?: ButtonType,

stateEffect?: boolean

}

见方法1参数说明。

属性

展开

名称

参数类型

描述

type

ButtonType

设置Button样式。

默认值:ButtonType.Capsule

stateEffect

boolean

按钮按下时是否开启按压态显示效果,当设置为false时,按压效果关闭。

默认值:true

ButtonType枚举说明

展开

名称

描述

Capsule

胶囊型按钮(圆角默认为高度的一半)。

Circle

圆形按钮。

Normal

普通按钮(默认不带圆角)。

说明
  • 按钮圆角通过通用属性borderRadius(不支持通过border接口设置圆角)。
  • 当按钮类型为Capsule时,borderRadius设置不生效,按钮圆角始终为高度的一半。
  • 当按钮类型为Circle时,borderRadius即为按钮半径,若未设置borderRadius按钮半径则为宽、高中较小值的一半。
  • 按钮文本通过通用文本样式进行设置。

示例

收起
自动换行
深色代码主题
复制
  1. // xxx.ets
  2. @Entry
  3. @Component
  4. struct ButtonExample {
  5. build() {
  6. Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Start, justifyContent: FlexAlign.SpaceBetween }) {
  7. Text('Normal button').fontSize(9).fontColor(0xCCCCCC)
  8. Flex({ alignItems: ItemAlign.Center, justifyContent: FlexAlign.SpaceBetween }) {
  9. Button('OK', { type: ButtonType.Normal, stateEffect: true }).borderRadius(8).backgroundColor(0x317aff).width(90)
  10. Button({ type: ButtonType.Normal, stateEffect: true }) {
  11. Row() {
  12. LoadingProgress().width(20).height(20).margin({ left: 12 }).color(0xFFFFFF)
  13. Text('loading').fontSize(12).fontColor(0xffffff).margin({ left: 5, right: 12 })
  14. }.alignItems(VerticalAlign.Center)
  15. }.borderRadius(8).backgroundColor(0x317aff).width(90).height(40)
  16. Button('Disable', { type: ButtonType.Normal, stateEffect: false }).opacity(0.4)
  17. .borderRadius(8).backgroundColor(0x317aff).width(90)
  18. }
  19. Text('Capsule button').fontSize(9).fontColor(0xCCCCCC)
  20. Flex({ alignItems: ItemAlign.Center, justifyContent: FlexAlign.SpaceBetween }) {
  21. Button('OK', { type: ButtonType.Capsule, stateEffect: true }).backgroundColor(0x317aff).width(90)
  22. Button({ type: ButtonType.Capsule, stateEffect: true }) {
  23. Row() {
  24. LoadingProgress().width(20).height(20).margin({ left: 12 }).color(0xFFFFFF)
  25. Text('loading').fontSize(12).fontColor(0xffffff).margin({ left: 5, right: 12 })
  26. }.alignItems(VerticalAlign.Center).width(90).height(40)
  27. }.backgroundColor(0x317aff)
  28. Button('Disable', { type: ButtonType.Capsule, stateEffect: false }).opacity(0.4)
  29. .backgroundColor(0x317aff).width(90)
  30. }
  31. Text('Circle button').fontSize(9).fontColor(0xCCCCCC)
  32. Flex({ alignItems: ItemAlign.Center, wrap: FlexWrap.Wrap }) {
  33. Button({ type: ButtonType.Circle, stateEffect: true }) {
  34. LoadingProgress().width(20).height(20).color(0xFFFFFF)
  35. }.width(55).height(55).backgroundColor(0x317aff)
  36. Button({ type: ButtonType.Circle, stateEffect: true }) {
  37. LoadingProgress().width(20).height(20).color(0xFFFFFF)
  38. }.width(55).height(55).margin({ left: 20 }).backgroundColor(0xF55A42)
  39. }
  40. }.height(400).padding({ left: 35, right: 35, top: 35 })
  41. }
  42. }

搜索
请输入您想要搜索的关键词