按鈕組件

2024-01-22 17:05 更新

按鈕組件,可快速創(chuàng)建不同樣式的按鈕。

說明

該組件從API Version 7開始支持。后續(xù)版本如有新增內(nèi)容,則采用上角標單獨標記該內(nèi)容的起始版本。

子組件

可以包含單個子組件。

接口

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

從API version 9開始,該接口支持在ArkTS卡片中使用。

參數(shù):

參數(shù)名

參數(shù)類型

必填

參數(shù)描述

type

ButtonType

描述按鈕顯示樣式。

默認值:ButtonType.Capsule

stateEffect

boolean

按鈕按下時是否開啟按壓態(tài)顯示效果,當設置為false時,按壓效果關閉。

默認值:true

說明:當開啟按壓態(tài)顯示效果,開發(fā)者設置狀態(tài)樣式時,會基于狀態(tài)樣式設置完成后的背景色再進行顏色疊加。

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

使用文本內(nèi)容創(chuàng)建相應的按鈕組件,此時Button無法包含子組件。

從API version 9開始,該接口支持在ArkTS卡片中使用。

參數(shù):

參數(shù)名

參數(shù)類型

必填

參數(shù)描述

label

ResourceStr

按鈕文本內(nèi)容。

options

{ type?: ButtonType, stateEffect?: boolean }

見方法1參數(shù)說明。

屬性

除支持通用屬性外,還支持以下屬性:

名稱

參數(shù)類型

描述

type

ButtonType

設置Button樣式。

默認值:ButtonType.Capsule

從API version 9開始,該接口支持在ArkTS卡片中使用。

stateEffect

boolean

按鈕按下時是否開啟按壓態(tài)顯示效果,當設置為false時,按壓效果關閉。

默認值:true

從API version 9開始,該接口支持在ArkTS卡片中使用。

事件

支持通用事件。

ButtonType枚舉說明

從API version 9開始,該接口支持在ArkTS卡片中使用。

名稱

描述

Capsule

膠囊型按鈕(圓角默認為高度的一半)。

Circle

圓形按鈕。

Normal

普通按鈕(默認不帶圓角)。

說明
  • 按鈕圓角通過通用屬性borderRadius設置(不支持通過border接口設置圓角),且只支持設置一個相同的圓角。
  • 當按鈕類型為Capsule時,borderRadius設置不生效,按鈕圓角始終為寬、高中較小值的一半。
  • 當按鈕類型為Circle時,borderRadius即為按鈕半徑,若未設置borderRadius按鈕半徑則為寬、高中較小值的一半。
  • 按鈕文本通過通用文本樣式進行設置。
  • 設置顏色漸變需先設置backgroundColor為透明色。

示例

示例1

  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 })
  10. .borderRadius(8)
  11. .backgroundColor(0x317aff)
  12. .width(90)
  13. .onClick(() => {
  14. console.log('ButtonType.Normal')
  15. })
  16. Button({ type: ButtonType.Normal, stateEffect: true }) {
  17. Row() {
  18. LoadingProgress().width(20).height(20).margin({ left: 12 }).color(0xFFFFFF)
  19. Text('loading').fontSize(12).fontColor(0xffffff).margin({ left: 5, right: 12 })
  20. }.alignItems(VerticalAlign.Center)
  21. }.borderRadius(8).backgroundColor(0x317aff).width(90).height(40)
  22. Button('Disable', { type: ButtonType.Normal, stateEffect: false }).opacity(0.4)
  23. .borderRadius(8).backgroundColor(0x317aff).width(90)
  24. }
  25. Text('Capsule button').fontSize(9).fontColor(0xCCCCCC)
  26. Flex({ alignItems: ItemAlign.Center, justifyContent: FlexAlign.SpaceBetween }) {
  27. Button('OK', { type: ButtonType.Capsule, stateEffect: true }).backgroundColor(0x317aff).width(90)
  28. Button({ type: ButtonType.Capsule, stateEffect: true }) {
  29. Row() {
  30. LoadingProgress().width(20).height(20).margin({ left: 12 }).color(0xFFFFFF)
  31. Text('loading').fontSize(12).fontColor(0xffffff).margin({ left: 5, right: 12 })
  32. }.alignItems(VerticalAlign.Center).width(90).height(40)
  33. }.backgroundColor(0x317aff)
  34. Button('Disable', { type: ButtonType.Capsule, stateEffect: false }).opacity(0.4)
  35. .backgroundColor(0x317aff).width(90)
  36. }
  37. Text('Circle button').fontSize(9).fontColor(0xCCCCCC)
  38. Flex({ alignItems: ItemAlign.Center, wrap: FlexWrap.Wrap }) {
  39. Button({ type: ButtonType.Circle, stateEffect: true }) {
  40. LoadingProgress().width(20).height(20).color(0xFFFFFF)
  41. }.width(55).height(55).backgroundColor(0x317aff)
  42. Button({ type: ButtonType.Circle, stateEffect: true }) {
  43. LoadingProgress().width(20).height(20).color(0xFFFFFF)
  44. }.width(55).height(55).margin({ left: 20 }).backgroundColor(0xF55A42)
  45. }
  46. }.height(400).padding({ left: 35, right: 35, top: 35 })
  47. }
  48. }

示例2

  1. // xxx.ets
  2. @Entry
  3. @Component
  4. struct SwipeGestureExample {
  5. @State count: number = 0
  6. build() {
  7. Column() {
  8. Text(`${this.count}`)
  9. .fontSize(30)
  10. .onClick(() => {
  11. this.count++
  12. })
  13. if (this.count <= 0) {
  14. Button('count is negative').fontSize(30).height(50)
  15. } else if (this.count % 2 === 0) {
  16. Button('count is even').fontSize(30).height(50)
  17. } else {
  18. Button('count is odd').fontSize(30).height(50)
  19. }
  20. }.height('100%').width('100%').justifyContent(FlexAlign.Center)
  21. }
  22. }

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號