Android copy text content to system Clipboard (free copy)

Source: Internet
Author: User

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.