HarmonyOS鸿蒙Next中如何通过gestureStyle获取属性字符串中点击位置的字符

HarmonyOS鸿蒙Next中如何通过gestureStyle获取属性字符串中点击位置的字符 刚学鸿蒙望大家指点一二:

import { promptAction } from '@kit.ArkUI';

@Entry
@Component
struct Index {
  s: string = '运动35分钟'
  gestureStyle: GestureStyle = new GestureStyle({
    onClick: () => {promptAction.showToast({message: '怎么拿到对应点击位置的字符呢?'})}
  })
  mutableStyledString1: MutableStyledString = new MutableStyledString(this.s, [{
    styledKey: StyledStringKey.GESTURE,
    styledValue: this.gestureStyle
  }]);
  controller: TextController = new TextController();
  build() {
    Column() {
      Text(undefined, { controller: this.controller})
        .onAppear(() => {
          this.controller.setStyledString(this.mutableStyledString1);
        })
    }
    .width('100%')
    .backgroundColor(Color.Pink)
  }
}

更多关于HarmonyOS鸿蒙Next中如何通过gestureStyle获取属性字符串中点击位置的字符的实战教程也可以访问 https://www.itying.com/category-93-b0.html

1 回复

更多关于HarmonyOS鸿蒙Next中如何通过gestureStyle获取属性字符串中点击位置的字符的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next中,可以通过gestureStyle获取属性字符串中点击位置的字符。首先,使用Gesture组件监听点击事件,然后通过GestureEvent获取点击的坐标。接着,利用TextLayoutTextPainter计算点击位置对应的字符索引,最后从字符串中获取该字符。具体实现需结合Text组件和手势事件处理逻辑。

回到顶部