HarmonyOS 使用RichEdit怎么实现@功能,删除的时候能把@的用户整个删除

使用RichEdit怎么实现@功能,删除的时候能把@的用户整个删除?

HarmonyOS
2024-12-25 14:31:29
1.2w浏览
收藏 0
回答 2
回答 2
按赞同
/
按时间
凡尘一梦
1

使用 addBuildSpan实现

   this.richEditorController.addBuilderSpan(this.atBuilder)
  • 1.
 this.atBuilder = () => {
                        this.AtSpan(userId)
                    }
  • 1.
  • 2.
  • 3.
  @Builder
    AtSpan(userId: string) {
        if (userId == "all") {
            Text("@全体成员").height(30).padding({ right: 5 })

        } else {
            Text(`@${CacheViewModel.getInstance().getUserNameByUserId(Number(userId))}`)
                .height(30)
                .padding({ right: 5 })

        }

    }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.


分享
微博
QQ
微信
回复
2024-12-27 15:04:38
zbw_apple

参考示例如下:

@Entry
@Component
struct Index {
  controller: RichEditorController = new RichEditorController();
  option: RichEditorOptions = { controller: this.controller };
  @State message: string = "@test(dd)"
  @State content: string = ""
  private my_builder: CustomBuilder = () => {
    this.placeholderBuilder()
  }

  @Builder
  placeholderBuilder() {
    Row() {
      Text(this.message).fontSize(15)
        .margin({
          right: 10
        })
        .fontColor(Color.Blue)
    }
  }

  build() {
    Column() {
      Button("获取选择内容 " + this.content).onClick(() => {
        this.content = ""
        this.controller.getSpans().forEach(item => {
          if (typeof (item as RichEditorImageSpanResult)['imageStyle'] != 'undefined') {
            if ((item as RichEditorImageSpanResult).valueResourceStr == "") {
              console.info("builder span index " + (item as RichEditorImageSpanResult).spanPosition.spanIndex +
                ", range : " + (item as RichEditorImageSpanResult).offsetInSpan[0] + ", " +
              (item as RichEditorImageSpanResult).offsetInSpan[1] + ", size : " +
              (item as RichEditorImageSpanResult).imageStyle[0] + ", " +
              (item as RichEditorImageSpanResult).imageStyle[1])
            } else {
              console.info("image span " + (item as RichEditorImageSpanResult).valueResourceStr + ", index : " +
              (item as RichEditorImageSpanResult).spanPosition.spanIndex + ", range: " +
              (item as RichEditorImageSpanResult).offsetInSpan[0] + ", " +
              (item as RichEditorImageSpanResult).offsetInSpan[1] + ", size : " +
              (item as RichEditorImageSpanResult).imageStyle.size[0] + ", " +
              (item as RichEditorImageSpanResult).imageStyle.size[1])
            }
          } else {
            this.content += (item as RichEditorTextSpanResult).value;
            this.content += "\n"
            console.info("text span: " + (item as RichEditorTextSpanResult).value)
          }
        })
      })
      Column() {
        RichEditor(this.option)
          .onReady(() => {
            this.controller.addBuilderSpan(this.my_builder, { offset: 0 })
          })
          .width("100%")
          .height("30%")
      }
      .width("100%")
      .height("70%")
    }
  }
}
  • 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.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
分享
微博
QQ
微信
回复
2024-12-25 17:01:46


相关问题
Text如何实现删除线功能
1737浏览 • 1回复 待解决
【JS】如何实现左滑删除功能
3945浏览 • 1回复 待解决
如何实现ArrayList删除、去重等功能
879浏览 • 1回复 待解决
HarmonyOS元服务添加、删除功能
1403浏览 • 1回复 待解决
侧滑删除功能列表有哪些?
1250浏览 • 1回复 待解决
HarmonyOS 生成faultlog如何删除
525浏览 • 1回复 待解决