Directly on the code: (corresponding class: Android.content.ClipboardManager)
// get Clipboard Manager: Clipboardmanager cm = (clipboardmanager) Getsystemservice (context.clipboard_service); // Create a normal character type Clipdata Clipdata mclipdata = Clipdata.newplaintext ("Label"" here is the text to be copied " ); // put the Clipdata content in the system Clipboard. Cm.setprimaryclip (mclipdata);
The above code can put the text on the Clipboard, and then find an input box "paste" on the line ~
PS: Prior to API 11, copying to the system Clipboard was Cm.settext ("copied text"); but this sentence has expired, after API11 , the Clipboardmanager was upgraded, Change to Cm.setprimaryclip (Mclipdata) so that it can copy text, URIs, and intent through (clipdata). As in the example above;
***********************************************************************
If you want to freely copy the text of a control such as TextView, just add it to the control android:textisselectable= "true", or Java code Plus Settextisselectable (true); effect
************************************************************************
The following are detailed below:
(1) Get Clipboard Manager:
Clipboardmanager Mclipboardmanager = (clipboardmanager) getsystemservice (Context.clipboard_service);
(2) Create a Clipdata object that can be stored in the Clipboard (the Clipdata object contains one or more Clipdata.item objects):
A. create A normal character type Clipdata:
Clipdata mclipdata =clipdata.newplaintext ("Label""Content" ); // ' label ' This is any text tag
B. Create a URL-type clipdata:
Clipdata.newrawuri ("Label", Uri.parse ("http://www.baidu.com" ));
C. Create a intent type clipdata:
Note: The above three methods create only one Clipdata.item object in the Clipdata object, and if you want to add more than one item to the Clipdata object , you should pass the Clipdata object's addItem () Method is added.
(3) Copy the Clipdata data to the Clipboard:
Clipboardmanager.setprimaryclip (Clipdata object);
(4) Get Clipdata data from the Clipboard:
Clipboardmanager.getprimaryclip ();
PS: normal string, URL and intent three data usage:
(1) Ordinary characters: is the ordinary string cut, copy, paste.
(2) URL: In the copy of the time can be copied a URL, this URL can be requested ContentProvider URL, at the time of pasting call Contntprovider get data, and use.
(3) Intent: For example, in the Notepad application long press A record entry, this will create a Intent to delete the Notepad, and add to the Clipboard, when the user dragged this Notepad entry to the Trash can release, the app will get Intent from the Clipboard and execute, this Notepad entry is deleted.
Android copy text content to system Clipboard (free copy)