不同狀態(tài)下的樣式

2024-01-22 16:24 更新

多態(tài)樣式

說明

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

屬性

名稱

參數(shù)類型

描述

stateStyles

StateStyles

設(shè)置組件不同狀態(tài)的樣式。

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

StateStyles接口說明

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

名稱

類型

必填

描述

normal

()=>void

組件無狀態(tài)時(shí)的樣式。

pressed

()=>void

組件按下狀態(tài)的樣式。

disabled

()=>void

組件禁用狀態(tài)的樣式。

focused

()=>void

組件獲焦?fàn)顟B(tài)的樣式。

clicked

()=>void

組件點(diǎn)擊狀態(tài)的樣式。

示例

  1. // xxx.ets
  2. @Entry
  3. @Component
  4. struct StyleExample {
  5. @State isEnable: boolean = true
  6. @Styles pressedStyles() {
  7. .backgroundColor("#ED6F21")
  8. .borderRadius(10)
  9. .borderStyle(BorderStyle.Dashed)
  10. .borderWidth(2)
  11. .borderColor("#33000000")
  12. .width(120)
  13. .height(30)
  14. .opacity(1)
  15. }
  16. @Styles disabledStyles() {
  17. .backgroundColor("#E5E5E5")
  18. .borderRadius(10)
  19. .borderStyle(BorderStyle.Solid)
  20. .borderWidth(2)
  21. .borderColor("#2a4c1919")
  22. .width(90)
  23. .height(25)
  24. .opacity(1)
  25. }
  26. @Styles normalStyles() {
  27. .backgroundColor("#0A59F7")
  28. .borderRadius(10)
  29. .borderStyle(BorderStyle.Solid)
  30. .borderWidth(2)
  31. .borderColor("#33000000")
  32. .width(100)
  33. .height(25)
  34. .opacity(1)
  35. }
  36. build() {
  37. Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center }) {
  38. Text("normal")
  39. .fontSize(14)
  40. .fontColor(Color.White)
  41. .opacity(0.5)
  42. .stateStyles({
  43. normal: this.normalStyles,
  44. })
  45. .margin({ bottom: 20 })
  46. .textAlign(TextAlign.Center)
  47. Text("pressed")
  48. .backgroundColor("#0A59F7")
  49. .borderRadius(20)
  50. .borderStyle(BorderStyle.Dotted)
  51. .borderWidth(2)
  52. .borderColor(Color.Red)
  53. .width(100)
  54. .height(25)
  55. .opacity(1)
  56. .fontSize(14)
  57. .fontColor(Color.White)
  58. .stateStyles({
  59. pressed: this.pressedStyles,
  60. })
  61. .margin({ bottom: 20 })
  62. .textAlign(TextAlign.Center)
  63. Text(this.isEnable == true ? "effective" : "disabled")
  64. .backgroundColor("#0A59F7")
  65. .borderRadius(20)
  66. .borderStyle(BorderStyle.Solid)
  67. .borderWidth(2)
  68. .borderColor(Color.Gray)
  69. .width(100)
  70. .height(25)
  71. .opacity(1)
  72. .fontSize(14)
  73. .fontColor(Color.White)
  74. .enabled(this.isEnable)
  75. .stateStyles({
  76. disabled: this.disabledStyles,
  77. })
  78. .textAlign(TextAlign.Center)
  79. Text("control disabled")
  80. .onClick(() => {
  81. this.isEnable = !this.isEnable
  82. console.log(`${this.isEnable}`)
  83. })
  84. .fontSize(10)
  85. }
  86. .width(350).height(300).margin(10)
  87. }
  88. }


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

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)