HarmonyOS 文本选择的手柄在拖拽过程中,有没有办法感知到拖拽结束

有两个问题:

1. 文本选择的手柄在拖拽过程中,onTextSelectionChange会一直收到回调,有没什么办法感知到拖拽结束,我这边有个需求是文本全选和部分选择时弹框界面会切换,因此希望在拖拽结束时再做这个操作。

2. TextController的closeSelectionMenu只能够关闭选择手柄,文本的选中态不会去掉,看下是否符合预期。

HarmonyOS
2024-08-10 12:04:15
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zbw_apple

问题1.通过onTextSelectionChange可以拿到起始下标和终点下标,再和文本的起始下标和终点下标对比,就可以确认为全选了。

问题2. 确认closeSelectionMenu关闭自定义选择菜单时,文本选中状态不会去掉这是个规格,只能再次点击Text组件区域去掉选中状态。

@Entry 
@Component 
struct Demo { 
  controller: TextController = new TextController(); 
  options: TextOptions = { controller: this.controller }; 
  @State start: number = -1 
  @State end: number = -1 
  build() { 
    Column() { 
      Column() { 
        Text(undefined, this.options) { 
          Span('Hello World') 
          ImageSpan($r('app.media.icon')) 
            .width('100px') 
            .height('100px') 
            .objectFit(ImageFit.Fill) 
            .verticalAlign(ImageSpanAlignment.CENTER) 
        } 
        .selection(this.start,this.end) 
        .copyOption(CopyOptions.InApp) 
        .bindSelectionMenu(TextSpanType.IMAGE, this.LongPressImageCustomMenu, TextResponseType.LONG_PRESS, { 
          onDisappear: () => { 
            console.info(`自定义选择菜单关闭时回调`); 
          }, 
          onAppear: () => { 
            console.info(`自定义选择菜单弹出时回调`); 
          } 
        }) 
        .bindSelectionMenu(TextSpanType.TEXT, this.RightClickTextCustomMenu, TextResponseType.RIGHT_CLICK) 
        .bindSelectionMenu(TextSpanType.MIXED, this.SelectMixCustomMenu, TextResponseType.SELECT) 
        .onTextSelectionChange((selectionStart: number, selectionEnd: number) => { 
          this.start = selectionStart 
          this.end = selectionEnd 
          console.info(`文本选中区域变化回调, selectionStart: ${selectionStart}, selectionEnd: ${selectionEnd}`); 
        }) 
        .borderWidth(1) 
        .borderColor(Color.Red) 
        .width(200) 
        .height(100) 
      } 
      .width('100%') 
      .backgroundColor(Color.White) 
      .alignItems(HorizontalAlign.Start) 
      .padding(25) 
    } 
    .height('100%') 
  } 
 
  @Builder 
  RightClickTextCustomMenu() { 
    Column() { 
      Menu() { 
        MenuItemGroup() { 
          MenuItem({ startIcon: $r('app.media.app_icon'), content: "Right Click Menu 1", labelInfo: "" }) 
            .onClick((event) => { 
              this.start = -1 
              this.end = -1 
              this.controller.closeSelectionMenu(); 
            }) 
 
          MenuItem({ startIcon: $r('app.media.app_icon'), content: "Select Mixed Menu 2", labelInfo: "" }) 
          MenuItem({ startIcon: $r('app.media.app_icon'), content: "Select Mixed Menu 3", labelInfo: "" }) 
        } 
      } 
      .MenuStyles() 
    } 
  } 
} 
 
@Extend(Menu) 
function MenuStyles() { 
  .radius($r('sys.float.ohos_id_corner_radius_card')) 
  .clip(true) 
  .backgroundColor('#F0F0F0') 
}
  • 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.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
分享
微博
QQ
微信
回复
2024-08-10 17:21:31
相关问题
HarmonyOS 有没有办法动态添加组件?
1868浏览 • 1回复 待解决
HarmonyOS 有没有办法截取webvIew长图
759浏览 • 1回复 待解决