在HarmonyOS NEXT 中系统剪贴板的复制粘贴方法?

阅读 686
1 个回答

你可以参考如下示例代码:

//设置剪贴板数据 
export function SetPasteboardData() { 
  let plainTextData = new unifiedDataChannel.UnifiedData(); 
  let plainText = new unifiedDataChannel.PlainText(); 
  plainText.details = { 
    Key: 'test1234', 
    Value: 'test1234', 
  }; 
  plainText.textContent = 'test1234'; 
  plainText.abstract = 'test1234'; 
  plainTextData.addRecord(plainText); 
 
  let systemPasteboard: pasteboard.SystemPasteboard = pasteboard.getSystemPasteboard(); 
  try { 
    systemPasteboard.setUnifiedDataSync(plainTextData); 
    console.info('tag, Succeeded in setting UnifiedData.'); 
  } catch (err) { 
    console.error('tag, Failed to set UnifiedData. Cause:' + err.message); 
  }; 
} 
 
//获取剪贴板数据 
export function GetPasteboardData() { 
  let systemPasteboard: pasteboard.SystemPasteboard = pasteboard.getSystemPasteboard(); 
  try { 
    let result: unifiedDataChannel.UnifiedData = systemPasteboard.getUnifiedDataSync(); 
    console.info('tag, Succeeded in getting UnifiedData.'); 
  } catch (err) { 
    console.error('tag, Failed to get UnifiedData. Cause:' + err.message); 
  }; 
}

剪贴板的API和示例请参考如下文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-refere...

本文参与了 【 HarmonyOS NEXT 技术问答冲榜,等你来战!】欢迎正在阅读的你也加入。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题