CopyText.ets 698 B

123456789101112131415161718192021222324
  1. import { IBestToast } from '@ibestservices/ibest-ui';
  2. import { pasteboard } from '@kit.BasicServicesKit';
  3. export function copyText(text: string, showToast: boolean = true) {
  4. const pasteboardData = pasteboard.createData(pasteboard.MIMETYPE_TEXT_PLAIN, text);
  5. const systemPasteboard = pasteboard.getSystemPasteboard();
  6. systemPasteboard.setData(pasteboardData); // 将数据放入剪切板
  7. systemPasteboard.getData().then((data) => {
  8. if (data) {
  9. if (showToast) {
  10. IBestToast.show({
  11. type: "success",
  12. message: "复制成功"
  13. })
  14. }
  15. } else {
  16. IBestToast.show({
  17. type: "fail",
  18. message: "复制失败"
  19. })
  20. }
  21. })
  22. }