中国优质的IT技术网站
专业IT技术创作平台
IT职业在线教育平台
微信扫码分享
import pasteboard from '@ohos.pasteboard' import promptAction from '@ohos.promptAction' @Entry @Component export struct CopyText { private textContent: string = "复制我" build() { Column() { Text(this.textContent) .fontSize($r("sys.float.ohos_id_text_size_body3")) .borderRadius(9) .borderWidth(1) .padding({ left: 8, right:8}) .fontColor($r('sys.color.ohos_id_color_text_primary')) .fontWeight(FontWeight.Medium) .opacity($r("sys.float.ohos_id_alpha_content_secondary")) .onClick(() => copyText(this.textContent)) } } } function copyText(text: string) { const pasteboardData = pasteboard.createData(pasteboard.MIMETYPE_TEXT_PLAIN, text) const systemPasteboard = pasteboard.getSystemPasteboard() systemPasteboard.setData(pasteboardData) // 将数据放入剪切板 systemPasteboard.getData().then((data) => { if (data) { promptAction.showToast({ message: "复制成功" }) } else { promptAction.showToast({ message: "复制失败" }) } }) }