Android text and input-copy and paste (2)

Source: Internet
Author: User

Clipboard-related classes

Clipboardmanager

In Android, the system clipboard is represented by a global clipboardmanager class. You cannot directly instantiate this class. On the contrary, you must call the getsystemservice (clipboard_service) method to obtain reference to it.

Clipdata, clipdata. item, and clipdescription

To add data to the clipboard, you need to create a clipdata object that contains the data description and data itself. The clipboard holds only one clipdata object each time. The clipdata object contains a clipdescription object and one or more clipdata. Item objects.

The clipdescription object contains metadata about clip, especially an array of MIME types that are valid for the Clipboard data. When you place a clipboard object, this array is valid for the app that executes the paste processing, you can use it to check whether the paste application can process these valid MIME types.

The clipdata. item object contains text, Uri, or intent data:

Text is a character string of the charsequence type.

A URI is a URI object. Although any type of URI is allowed, this object usually contains the URI of a content provider. The application that provides data puts the URI on the clipboard. The application that wants to paste the data will obtain the URI from the clipboard and use it to access the content provider (or other data sources) to obtain the data.

Intent is an intent object. This type allows you to copy the shortcut of an application to the clipboard. Then, you can save the shortcut to their applications for future use.

You can add multiple clipdata. Item objects to the clipboard. This allows you to select multiple options as a separate clipboard object. For example, if you have a list window that allows you to select multiple list items at a time, you can copy all the selected items to the clipboard at a time. To do this, you need to create a clipdata. Item pair for each list item, and then add each clipdata. item object to the clipdata object.

Clipdata Class Method

The clipdata class provides static convenience for creating a clipdata object with a clipdata. item object and a simple clipdescription object:

Newplaintext (Label, text)

This method returns a clipdata object with a clipdata. item object containing a text string. The label of the clipdescription object is set to label, and the MIME type is mimetype_text_plain.

Use newplaintext () to create a clipboard object of the text string type.

Newuri (Resolver, label, Uri)

This method returns a clipdata object with a clipdata. item object containing a URI. The label of the clipdescription object is set to label. If the URI is a content uri (URI. the getscheme () method returns content :), so this method uses the contentresolver object provided in the resolver parameter to obtain valid MIME types from the content provider and save them in the clipdescription object. For a URI that is not the content type, this method sets the MIME type to mimetype_text_urilist.

Use the newuri () method to create a clipboard object from a URI, especially content: type.

Newintent (Label, intent)

This method returns a clipdata object with a separate clipdata. item object containing the intent object. The clipdescription Object Label is set to label, and the MIME type is mimetype_text_intent.

Use the newintent () method to create a clipboard object from an intent object.

Convert Clipboard data to text

Even if your application only processes text, you can use the clipdata. item. coercetotext () method to convert non-text data from the clipboard to text.

This method converts the data in clipdata. item into text and returns a charsequence string. The clipdata. item. coercetotext () method returns the text value according to the data format in clipdata. item:

Text: If clipdata. item is text (the return value of the gettext () method is not null), The coercetotext () method returns the text.

Uri: If clipdata. item is a URI (the return value of the geturi () method is not null), The coercetotext () method will try to use it as the URI of the content:

1. If the URI is a content Uri and its provider can return a text stream, the coercetotext () method returns a text stream.

2. If the URI is a content Uri, but its provider cannot provide a text stream, the coercetotext () method returns the URI expression. This expression returns the same value as the URI. tostring () method.

3. If the URI is not a content Uri, The coercetotext () method returns the URI expression. This expression returns the same value as the URI. tostring () method.

Intent: If clipdata. item is an intent object (the return value of the getintent () method is not null), The coercetotext () method converts it into an intent Uri and returns it. This expression returns the same value as the intent. touri (uri_intent_scheme) method.

The clipboard frame is like 1.

Figure 1. Android clipboard framework

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.