Copy and paste in Android

Source: Internet
Author: User

Copy and paste in Android
Copy and paste in Android

 

The Clipboard Framework

When clipboard framework is used, data is stored in a clip object, which is then placed in the system clipboard.

  Clip object can be in three forms:

  Text: a Text string.

Text is directly placed in the clip object, and then placed in the clipboard. When you paste the string, you can directly get the object from the clipboard and put the string into your app storage.

  URI:UriObject.

Indicates any URI. This form is mainly used to copy complex data from a content provider.

CopyUriThe object is placed in a clip object, and then placed in the clipboard. When the clip object is pasted, The Uri is obtained and parsed as a data resource such as content provider, then, copy the data from the resource to the application storage.

  Intent:IntentObject.

This supports the copy application shortcut.

When copying, place the Intent object in the clip object and then the clipboard. When pasting data, the Intent object is obtained from the clip object and placed in the application storage area.

 

  The clipboard holds only one clip object each time. When the application puts another clip object in, the previous one disappears.

Clipboard class

ClipboardManager

ClipboardManagerIndicates the system clipboard.GetSystemService (CLIPBOARD_SERVICE).

Full name:Android. text. ClipboardManagerIt has been abandoned since API 11.

It is replaced by its subclass:Android. content. ClipboardManager(Since API Level 11 ).

 

ClipData, ClipDescription, and ClipData. Item

The clip object mentioned above isClipDataClass, which contains ClipDescriptionObject and one or moreClipData.ItemObject.

ClipDescriptionThe object contains an array describing the MIME type of the clip object.

ClipData. ItemThe object contains text, URI, or Intent data.A clip object can contain one or more Item objects..

 

For example, if you want to copy multiple pieces of data in the list, you can createClipData. ItemObject, and then put them intoClipDataObject, so that multiple pieces of data are put in the clipboard at a time.

Note that the ClipData class is only available in API 11.

 

The concise method in ClipData

ClipDataClass has some static and concise methods for creating only oneClipData. ItemAnd a simple description ( ClipDescription.

NewPlainText (label, text)Return the ClipData object. The data is text, the description is label, and the MIME type isMIMETYPE_TEXT_PLAIN.

Similar:

NewUri (resolver, label, URI)

NewIntent (label, intent)

 

 

Convert the data in the clipboard into text

Non-text data in the clipboard can be passed throughClipData. Item. coerceToText ()Method to text processing.

1. This method first checks whether the item contains text. If so, the system returns the result directly.

2. Do not include text, and check whether there is a URI:

If the URI is a content URI and the provider returns the text stream, coerceToText () returns the text stream;

If the provider does not return a text stream or the URI is not a content URI at all, the coerceToText () method returns the URI expression, that isUri.toString().

3. Finally, if this item does not contain text or URI, it should contain Intent,coerceToText()The method will convert the Intent object into an Intent URI to return, andIntent.toUri(URI_INTENT_SCHEME)Same.

 

Android clipboard framework Summary

  

 

 

Copy to clipboard

  1. First, obtain the clipboard service:

// Gets a handle to the clipboard service.ClipboardManager clipboard = (ClipboardManager)        getSystemService(Context.CLIPBOARD_SERVICE);

 

  2. Put the data in the ClipData object.

Text:

// Creates a new text clip to put on the clipboardClipData clip = ClipData.newPlainText(simple text,Hello, World!);

URI:

// Creates a Uri based on a base Uri and a record ID based on the contact's last name// Declares the base URI stringprivate static final String CONTACTS = content://com.example.contacts;// Declares a path string for URIs that you use to copy dataprivate static final String COPY_PATH = /copy;// Declares the Uri to paste to the clipboardUri copyUri = Uri.parse(CONTACTS + COPY_PATH + / + lastName);...// Creates a new URI clip object. The system uses the anonymous getContentResolver() object to// get MIME types from provider. The clip object's label is URI, and its data is// the Uri previously created.ClipData clip = ClipData.newUri(getContentResolver(),URI,copyUri);

For Intent:

// Creates the IntentIntent appIntent = new Intent(this, com.example.demo.myapplication.class);...// Creates a clip object with the Intent in it. Its label is Intent and its data is// the Intent object created previouslyClipData clip = ClipData.newIntent(Intent,appIntent);

 

  3. Place the clip object in the clipboard:

// Set the clipboard's primary clip.clipboard.setPrimaryClip(clip);
 
Paste from clipboard

Take text pasting as an example.

Example:

Package com. example. helloclipboard; import android. OS. bundle; import android. app. activity; import android. content. clipData; import android. content. clipboardManager; import android. content. context; import android. util. log; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. editText; import android. widget. textView; import android. widget. toast; p Ublic class extends Activity {private EditText mEditText1 = null; private TextView mResultTextView = null; private ClipboardManager mClipboard = null; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_hello_clipboard_main); mResultTextView = (TextView) findViewById (R. id. textView1); mEditTe Xt1 = (EditText) findViewById (R. id. editText1); Button copyButton = (Button) findViewById (R. id. button1); Button pasteButton = (Button) findViewById (R. id. button2); copyButton. setOnClickListener (mOnClickListener); pasteButton. setOnClickListener (mOnClickListener);} private OnClickListener mOnClickListener = new OnClickListener () {@ Override public void onClick (View v) {switch (v. getId () {case R. id. Button1: copyFromEditText1 (); break; case R. id. button2: pasteToResult (); break; default: break ;}}; private void copyFromEditText1 () {// Gets a handle to the clipboard service. if (null = mClipboard) {mClipboard = (ClipboardManager) getSystemService (Context. CLIPBOARD_SERVICE);} // Creates a new text clip to put on the clipboard ClipData clip = ClipData. newPlainText (simple text, mEditText1. GetText (); // Set the clipboard's primary clip. mClipboard. setPrimaryClip (clip);} private void pasteToResult () {// Gets a handle to the clipboard service. if (null = mClipboard) {mClipboard = (ClipboardManager) getSystemService (Context. CLIPBOARD_SERVICE);} String resultString =; // check whether the Clipboard contains content if (! MClipboard. hasPrimaryClip () {Toast. makeText (HelloClipboardMainActivity. this, Clipboard is empty, Toast. LENGTH_SHORT ). show ();} else {ClipData clipData = mClipboard. getPrimaryClip (); int count = clipData. getItemCount (); for (int I = 0; I <count; ++ I) {ClipData. item item = clipData. getItemAt (I); CharSequence str = item. coerceToText (HelloClipboardMainActivity. this); Log. I (mengdd, item: + I ++: + str); resultString + = str ;}} mResultTextView. setText (resultString );}}

 

 

Design an effective copy and paste Function

To design an effective copy and paste function, pay attention to the following points:

1. At any time, only one clip object is stored in the clipboard.

The new copy operation will overwrite the previous clip object, because the user may exit from your application and copy one thing from other applications, therefore, you cannot assume that the last thing copied by the user in your application must be stored in the clipboard.

 

2. A clip object, that is, multipleClipData.ItemObjects are copied and pasted to support multiple options, rather than multiple single-choice forms.

You usually need all the projects in the clip object, that is, ClipData. Item has the same form, for example, it is all text, both URI or both Intent, rather than mixed forms.

 

3. When you provide data, you can provide different MIME expressions.

Add the MIME types you supportClipDescriptionAnd then implement it in your content provider.

 

4. When you get data from the clipboard, your application has the responsibility to check the available MIME types and decide which one to use.

Even if you have a clip object in the clipboard and the user wants to paste it, your application may not need to paste it.

You should perform the paste operation when the MIME type is compatible. You can choose to usecoerceToText()Method to convert the pasted content to text.

If your application supports multiple types, you can select one of them.

 

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.