鸿蒙ohos复制文本到剪切板

264 阅读1分钟
import { pasteboard } from '@kit.BasicServicesKit';

@Entry
@Component
struct Home{
    @State message: string = 'Hello World'
    
    build(){
        Column(){
            Text(this.message)
                .onClick(() =>{
                    const pasteboardData =pasteboard.createData(pasteboard.MIMETYPE_TEXT_PLAIN, this.message)
                    const systemPasteboard = pasteboard.getSystemPasteboard()
                    systemPasteboard.setData(pasteboardData) //将数据放入剪切板
                    systemPasteboard.getData().then((data) => {
                      if (data) {
                        promptAction.showToast({ message: "复制成功" })
                      } else {
                        promptAction.showToast({ message: "复制失败" })
                      }
                    })
                })
        }
    }

}