HarmonyOS Next中提供方如何对外提供AttributeModifier接口的实现类?是否有对应实现代码?

阅读 603
1 个回答
  1. 提供方创建AttributeModifier接口的实现类。

    // src/main/ets/pages/CommonComponent.ets
    // 提供方创建自定类Class类,实现系统AttributeModifier接口
    export class MyButtonModifier implements AttributeModifier<ButtonAttribute> {
      private buttonType: ButtonType = ButtonType.Normal;
      private stateEffectValue: boolean = false;
    
      constructor() {
      }
    
      applyNormalAttribute(instance: ButtonAttribute): void {
     instance.stateEffect(this.stateEffectValue);
     instance.type(this.buttonType);
     // 设置默认样式
     instance.width(200);
     instance.height(50);
     instance.fontSize(20)
      }
    
      stateEffect(enable: boolean): MyButtonModifier {
     this.stateEffectValue = enable;
     return this;
      }
    
      type(type: ButtonType): MyButtonModifier {
     this.buttonType = type;
     return this;
      }
    }
  2. 使用方创建提供方的AttributeModifier实现类实例,并作为系统组件attributeModifier属性方法的参数传入。

    // src/main/ets/pages/CommonComponent.ets
    @Component
    struct Index {
      modifier = new MyButtonModifier()
     .stateEffect(true)
     .type(ButtonType.Capsule)
    
      build() {
     Row() {
       Button('Capsule Button')
         .attributeModifier(this.modifier)
     }
     .width('100%')
     .height('100%')
      }
    }

本文参与了 【 HarmonyOS NEXT 技术问答冲榜,等你来战!】欢迎正在阅读的你也加入。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进