chenqs

ref |> 添加长按复制到剪切板功能

1 import router from '@ohos.router'; 1 import router from '@ohos.router';
2 import { Params } from '../../../../../../../commons/wdRouter/oh_modules/wdBean/src/main/ets/bean/content/Params'; 2 import { Params } from '../../../../../../../commons/wdRouter/oh_modules/wdBean/src/main/ets/bean/content/Params';
3 -import { Logger } from 'wdKit'; 3 +import { Logger, ToastUtils } from 'wdKit';
4 import { CustomTitleUI } from 'wdComponent/src/main/ets/components/reusable/CustomTitleUI'; 4 import { CustomTitleUI } from 'wdComponent/src/main/ets/components/reusable/CustomTitleUI';
  5 +import {BusinessError, pasteboard} from '@kit.BasicServicesKit';
5 6
6 const TAG = 'H5TipsPage' 7 const TAG = 'H5TipsPage'
7 8
@@ -42,10 +43,35 @@ struct H5TipsPage { @@ -42,10 +43,35 @@ struct H5TipsPage {
42 .textAlign(TextAlign.Center) 43 .textAlign(TextAlign.Center)
43 .margin({left: 40, right: 40}) 44 .margin({left: 40, right: 40})
44 .height(40) 45 .height(40)
  46 + .gesture(
  47 + LongPressGesture()
  48 + .onAction((event: GestureEvent|undefined)=>{
  49 + if (event) {
  50 + this.longPressToCopy()
  51 + }
  52 + })
  53 + )
45 } 54 }
46 .backgroundColor(Color.White) 55 .backgroundColor(Color.White)
47 .width("100%") 56 .width("100%")
48 .height("100%") 57 .height("100%")
49 58
50 } 59 }
  60 +
  61 + private longPressToCopy() {
  62 +
  63 + // 获取系统剪贴板对象
  64 + let systemPasteboard = pasteboard.getSystemPasteboard();
  65 + // 创建一条纯文本类型的剪贴板内容对象createPlainTextData
  66 + let pasteData = pasteboard.createPlainTextData(this.webUrl);
  67 + pasteData.addTextRecord(this.webUrl);
  68 + // 将数据写入系统剪贴板
  69 + systemPasteboard.setData(pasteData).then(()=>{
  70 + // 存入成功,处理正常场景
  71 + ToastUtils.shortToast('复制成功')
  72 + }).catch((error: BusinessError) => {
  73 + // 处理异常场景
  74 + Logger.info(TAG, '长按文字存储失败:' + error);
  75 + });
  76 + }
51 } 77 }