Android implements text copy to clipboard function (Clipboardmanager)

Source: Internet
Author: User

Android also has a clipboard (Clipboardmanager) that can copy some useful text to the Clipboard so that the user can paste the place used, below is how to use

Note: When guiding the package

API 11 Before: Android.text.ClipboardManager
After API 11: Android.content.ClipboardManager

Copy CodeThe code is as follows:
/**
* Text copy function is implemented
* Add by Wangqianzhou
* @param content
*/
public static void Copy (String content, context context)
{
Get Clipboard Manager
Clipboardmanager CMB = (Clipboardmanager) context.getsystemservice (Context.clipboard_service);
Cmb.settext (Content.trim ());
}
/**
* Implement Paste function
* Add by Wangqianzhou
* @param context
* @return
*/
public static String paste (context context)
{
Get Clipboard Manager
Clipboardmanager CMB = (Clipboardmanager) context.getsystemservice (Context.clipboard_service);
Return Cmb.gettext (). toString (). Trim ();
}
First create a Clipboardmanager object CMB and associate it with the system Clipboard. You can then copy the contents of the string type to the Clipboard by using the SetText (charsequence text) function. In addition, the Clipboardmanager class provides an abstract charsequence GetText () function and an abstract Boolean hasText (), You can get the contents of the string in the Clipboard, and whether the query Clipboard currently holds content. The Clipboardmanager class has two versions, using only the Clipboard manager that can save strings from API Level 1, since Android 3.0 (API level 11) The new version of the Clipboardmanager class supports more features. Please see the official documentation for details.

android2.1 Previous versions use the following methods

Copy CodeThe code is as follows:
Iclipboard clip = IClipboard.Stub.asInterface (Servicemanager.getservice ("clipboard"));
Clip.getclipboardtext (). toString ();//Get copied content
Clip.setclipboardtext (text);//Set the contents of clipboard

Android implements text copy to clipboard function (Clipboardmanager)

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.