Android 复制文本、Intent、Uri到剪切板

Android 复制文本到剪切板

官方文档:https://developer.android.com/guide/topics/text/copy-paste#kotlin

private fun copyTextToClipboard(){
                val clipboard = anchor.context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
                /**
                 * @param label User-visible label for the clip data.
                 * @param text The actual text in the clip.
                 */
                val clip: ClipData = ClipData.newPlainText("用户可见的标签", "复制的内容")
                clipboard.primaryClip=clip
}

同样的可以复制Uri数据到剪切板,官方方法


    private fun copyUriToClipboard() {
        val clipboard = anchor.context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager

        // Creates a Uri based on a base Uri and a record ID based on the contact's last name
        // Declares the base URI string
        const val CONTACTS = "content://com.example.contacts"

        // Declares a path string for URIs that you use to copy data
        const val COPY_PATH = "/copy"

        // Declares the Uri to paste to the clipboard
        val copyUri: Uri = Uri.parse("$CONTACTS$COPY_PATH/$lastName")

        ...

        // Creates a new URI clip object. The system uses the anonymous getContentResolver() object to
        // get MIME types from provider. The clip object's label is "URI", and its data is
        // the Uri previously created.
        val clip: ClipData = ClipData.newUri(contentResolver, "URI", copyUri)
        clipboard.primaryClip = clip
    }

复制intent


    private fun copyIntentToClipboard() {
        val clipboard = anchor.context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
        // Creates the Intent
        val appIntent = Intent(this, com.example.demo.myapplication::class.java)

        ...

        // Creates a clip object with the Intent in it. Its label is "Intent" and its data is   
        // the Intent object created previously
        val clip: ClipData = ClipData.newIntent("Intent", appIntent)
        clipboard.primaryClip = clip
    }

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值