HarmonyOS 现在是否除了button其他控件都不能触发applyPressedAttribute -

HarmonyOS
2024-12-25 13:09:58
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
put_get

参考文档中的instance参数支持范围:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-universal-attributes-attribute-modifier-V5

参考示例如下:

// xxx.ets
class MyButtonModifier implements AttributeModifier<ButtonAttribute> {
  applyNormalAttribute(instance: ButtonAttribute): void {
    instance.backgroundColor(Color.Black)
  }

  applyPressedAttribute(instance: ButtonAttribute): void {
    instance.backgroundColor(Color.Red)
  }
}

class MyTextModifier implements AttributeModifier<TextAttribute> {
  applyNormalAttribute(instance: TextAttribute): void {
    instance.backgroundColor(Color.Black)
  }

  applyPressedAttribute(instance: TextAttribute): void {
    instance.backgroundColor(Color.Red)
  }
}

@Entry
@Component
struct attributePressedDemo {
  @State modifier: MyButtonModifier = new MyButtonModifier()
  @State modifierText: MyTextModifier = new MyTextModifier()

  build() {
    Row() {
      Column() {
        Button("Button")
          .attributeModifier(this.modifier)
          .margin(20)
        Text("Text")
          .attributeModifier(this.modifierText)
          .width(100)
          .height(60)
          .margin(20)
          .fontColor(Color.White)
      }
      .width('100%')
    }
    .height('100%')
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
分享
微博
QQ
微信
回复
2024-12-25 15:23:51
相关问题
现在是否支持Webp动图?
3148浏览 • 1回复 待解决
HarmonyOS现在是否有deepLink方案
1061浏览 • 1回复 待解决
动图除了GIF,是否还支持其他格式
2861浏览 • 1回复 待解决
Button控件设置点击效果
1739浏览 • 1回复 待解决
现在不能直接创建.ts的文件吗?
913浏览 • 1回复 待解决