1、第一个方法
附上官方的连接
androidx.compose.ui.platform | Android Developers
就是利用compose工具包里面 LocalClipboardManager
androidx.compose.ui.platform | Android Developers
LocalClipboardManager 这个可以适用于compose工具包里面的内容,可以和剪贴板进行操作
具体用法如下,非常简单
具体用法如下
val current = LocalClipboardManager.current
current.setText(AnnotatedString("mutableSetOfText"))
这是官方的源代码
interface ClipboardManager {
/**
* This method put the text into the Clipboard.
*
* @param annotatedString The [AnnotatedString] to be put into Clipboard.
*/
@Suppress("GetterSetterNames")
fun setText(annotatedString: AnnotatedString)
/**
* This method get the text from the Clipboard.
*
* @return The text in the Clipboard.
* It could be null due to 2 reasons: 1. Clipboard is empty; 2. Cannot convert the
* [CharSequence] text in Clipboard to [AnnotatedString].
*/
fun getText(): AnnotatedString?
/**
* This method returns true if there is a text in the Clipboard, false otherwise.
*/
fun hasText(): Boolean = getText()?.isNotEmpty() == true
2、第二个方法
,相比第一个方法比较麻烦,要利用 安卓API操作
// 创建剪贴板数据
val clipData = ClipData.newPlainText("text/plain", mutableSetOfText)
// 获取 ClipboardManager 实例
val clipboardManager = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
// 设置剪贴板的主要内容
clipboardManager.setPrimaryClip(clipData)
上面的这两个方法都可以在compose状态下对剪贴板进行相应的操作,
上面内容都是自己在学习过程中搜集