Android implements text copying to the Clipboard function (Clipboardmanager) _android

Source: Internet
Author: User
Tags gettext versions

Note: When the guide bag

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

Copy Code code as follows:

/**
* Implement text copy function
* 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. The SetText (charsequence text) function can then be used to copy string content to the Clipboard. In addition, the Clipboardmanager class also provides an abstract charsequence GetText () function and an abstract Boolean hasText (). You can get the contents of the string on the Clipboard and whether the Clipboard is currently saved. There are two versions of the Clipboardmanager class, which uses a clipboard manager that can only save strings from API Level 1, since the Android 3.0 (API level 11 The new version of the Clipboardmanager class supports more features. Please refer to the official documentation for details.

android2.1 Previous versions use the following methods

Copy Code code as follows:

Iclipboard clip = IClipboard.Stub.asInterface (Servicemanager.getservice ("clipboard"));
Clip.getclipboardtext (). toString ();//Get copied content
Clip.setclipboardtext (text);/Set clipboard content

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.