From e37c900c2b6129db7003b1de33e71c41e5b84116 Mon Sep 17 00:00:00 2001 From: wangchensu Date: Fri, 27 Jun 2025 02:38:04 +0000 Subject: [PATCH] update zh-cn/application-dev/reference/apis-arkui/js-apis-arkui-AttributeUpdater.md. Signed-off-by: wangchensu --- .../js-apis-arkui-AttributeUpdater.md | 43 ++++++++++++++++--- 1 file changed, 37 insertions(+), 6 deletions(-) diff --git a/zh-cn/application-dev/reference/apis-arkui/js-apis-arkui-AttributeUpdater.md b/zh-cn/application-dev/reference/apis-arkui/js-apis-arkui-AttributeUpdater.md index 4d7d121eba3..97deaecdac0 100644 --- a/zh-cn/application-dev/reference/apis-arkui/js-apis-arkui-AttributeUpdater.md +++ b/zh-cn/application-dev/reference/apis-arkui/js-apis-arkui-AttributeUpdater.md @@ -86,23 +86,41 @@ AttributeUpdater首次设置给组件时提供的样式。 import { AttributeUpdater } from '@kit.ArkUI'; class MyButtonModifier extends AttributeUpdater { + //该AttributeUpdater对象第一次使用的时候触发的回调 initializeModifier(instance: ButtonAttribute): void { - instance.backgroundColor('#ff2787d9') - .width('50%') - .height(30); + instance.backgroundColor('#ffd5d5d5') + .labelStyle({maxLines:3}) + .width('80%') + } + + //该AttributeUpdater对象后续使用或者更新的时候触发的回调 + applyNormalAttribute(instance: ButtonAttribute): void { + instance.borderWidth(1) } } @Entry @Component -struct updaterDemo1 { +struct updaterDemo2 { modifier: MyButtonModifier = new MyButtonModifier(); + @State flushTheButton: string = "Button"; build() { Row() { Column() { - Button("Button") + Button(this.flushTheButton) .attributeModifier(this.modifier) + .onClick(() => { + //通过AttributeUpdater的attribute对属性进行修改 + //需要注意先通过组件的attributeModifier属性方法建立组件与AttributeUpdater绑定关系 + this.modifier.attribute?.backgroundColor('#ff2787d9').labelStyle({maxLines:5}); + }) + Button("Change The message to flush the Button") + .width('80%') + .labelStyle({maxLines:2}) + .onClick(() => { + this.flushTheButton = "Updates" + this.flushTheButton; + }) } .width('100%') } @@ -137,26 +155,39 @@ get attribute(): T | undefined import { AttributeUpdater } from '@kit.ArkUI'; class MyButtonModifier extends AttributeUpdater { + //该AttributeUpdater对象第一次使用的时候触发的回调 initializeModifier(instance: ButtonAttribute): void { instance.backgroundColor('#ffd5d5d5') .width('50%') .height(30); } + + //该AttributeUpdater对象后续使用或者更新的时候触发的回调 + applyNormalAttribute(instance: ButtonAttribute): void { + instance.borderWidth(1) + } } @Entry @Component struct updaterDemo2 { modifier: MyButtonModifier = new MyButtonModifier(); + @State flushTheButton: string = "Button"; build() { Row() { Column() { - Button("Button") + Button(this.flushTheButton) .attributeModifier(this.modifier) .onClick(() => { this.modifier.attribute?.backgroundColor('#ff2787d9').width('30%'); }) + Button("Change The message to flush the Button") + .width('50%') + .height(30) + .onClick(() => { + this.flushTheButton = "Update " + this.flushTheButton; + }) } .width('100%') } -- Gitee