textview 复制粘贴
In this tutorial, we’ll implement the copy-paste feature on a TextView in our Android Application.
At the end of this article, you’d be able to copy a text to the Android System’s Clipboard and then paste that Clipboard data.
在本教程中,我们将在Android应用程序的TextView上实现复制粘贴功能。
在本文的结尾,您可以将文本复制到Android系统的剪贴板中,然后粘贴该剪贴板数据。
Andorid剪贴板–复制粘贴 (Andorid Clipboard – Copy Paste)
You must have noticed that EditText has a built in Clipboard Manager for Copy Paste functionality.
But a TextView doesn’t. So in order to allow Copy Pasting TextViews, we need to register the ContextMenu.
您必须已经注意到EditText具有内置的Clipboard Manager用于复制粘贴功能。
但是TextView却没有。 因此,为了允许复制粘贴TextView,我们需要注册ContextMenu。
Hence in order to receive the menu events, we need to registerForContextMenu
.
Once this is done, you can long-press on the TextView
to display the menu.
因此,为了接收菜单事件,我们需要registerForContextMenu
。
完成此操作后,您可以长按TextView
以显示菜单。
But where’s the menu?
但是菜单在哪里?
The menu is created in the onCreateContextMenu() method.
The menu items actions are set in the onContextItemSelected
method.
菜单是在onCreateContextMenu()方法中创建的。
菜单项操作在onContextItemSelected
方法中设置。
将文本复制到剪贴板 (Copying Text To Clipboard)
It’s easy to copy TextView text onto the Clipboard. You just need to set the ClipData type as newPlainText
and pass the string.
将TextView文本复制到剪贴板很容易。 您只需要将ClipData类型设置为newPla