HarmonyOS鸿蒙Next中属性字符串点击不跳转
HarmonyOS鸿蒙Next中属性字符串点击不跳转
在AttributeModifier中同时设置 applyPressedAttribute
和 applyNormalAttribute
出现问题。点击没有出现弹窗。
下面是样例:
import { promptAction } from '@kit.ArkUI'
class TextAttributeModifier implements AttributeModifier<TextAttribute> {
controller2: TextController = new TextController()
mutableStyledString: MutableStyledString = new MutableStyledString("运动15分钟", [{
start: 0,
length: 6,
styledKey: StyledStringKey.GESTURE,
styledValue: new GestureStyle({
onClick: () => {
promptAction.showToast({ message: 'clickGestureAttr object trigger click event' })
}
})
}])
constructor(controller1: TextController) {
this.controller2 = controller1
}
applyNormalAttribute(instance: TextAttribute): void {
instance.fontColor(Color.Blue)
this.controller2.setStyledString(this.mutableStyledString)
}
applyPressedAttribute(instance: TextAttribute): void {
instance.fontColor(Color.Red)
this.controller2.setStyledString(this.mutableStyledString)
}
}
@Entry
@Component
struct demo {
controller3: TextController = new TextController()
@State mod: TextAttributeModifier = new TextAttributeModifier(this.controller3)
build() {
Column() {
Text(undefined, { controller: this.controller3 })
.attributeModifier(this.mod)
}.padding(100)
}
}
怎么规避?
更多关于HarmonyOS鸿蒙Next中属性字符串点击不跳转的实战教程也可以访问 https://www.itying.com/category-93-b0.html
这个问题曾经在 HarmonyOS 5.0.2(14) 出现过,其他版本没有问题。
如果遇到了,规避措施是 AttributeModifier 中去掉 applyPressedAttribute,这样只会调用一次 controller.setStyledString
更多关于HarmonyOS鸿蒙Next中属性字符串点击不跳转的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS鸿蒙Next中,属性字符串点击不跳转可能是由于事件绑定未正确设置或跳转逻辑未实现。检查Text
组件的onClick
事件是否绑定,并确保跳转逻辑在事件处理函数中正确执行。若使用Router
进行页面跳转,需确认目标页面已注册且路径正确。
根据代码分析,问题可能出在同时使用applyPressedAttribute
和applyNormalAttribute
时覆盖了手势事件。建议检查以下几点:
- 确保
GestureStyle
的手势回调没有被attributeModifier
覆盖 - 尝试将手势事件定义移到组件层面而不是AttributeModifier中
- 检查
TextController
是否正确传递了手势事件
可以尝试修改为以下方式:
Text(undefined, { controller: this.controller3 })
.onClick(() => {
promptAction.showToast({ message: 'click event' })
})
.attributeModifier(this.mod)
如果仍需要保持原有结构,建议检查手势事件的冒泡机制是否被属性修改器影响。