android-Copy and Paste(text and input)

来源:互联网 发布:plc温度控制编程实例 编辑:程序博客网 时间:2024/06/09 18:22
Android provides a powerful clipboard-based framework for copying and pasting. It supports both simple and complex data types, including text strings, complex data structures, text and binary stream data, and even application assets. 
The Clipboard Framework(剪贴板框架)
》The clip object can take one of three forms:Text,URI,Intent
ClipData convenience methods


To copy data, an application puts a ClipData object on theClipboardManager global clipboard. The ClipData contains one or more ClipData.Item objects and oneClipDescription object. To paste data, an application gets the ClipData, gets its MIME type from theClipDescription, and gets the data either from the ClipData.Item or from the content provider referred to byClipData.Item.
case R.id.menu_copy:


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


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


This snippet constructs a URI by encoding a record ID onto the content URI for the provider. This technique is covered in more detail in the section Encoding an identifier on the URI:
// Creates a Uri based on a base Uri and a record ID based on the contact's last name
// Declares the base URI string
private static final String CONTACTS = "content://com.example.contacts";


// Declares a path string for URIs that you use to copy data
private static final String COPY_PATH = "/copy";


// Declares the Uri to paste to the clipboard
Uri 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 an Intent


This snippet constructs an Intent for an application and then puts it in the clip object:
// Creates the Intent
Intent 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 previously
ClipData clip = ClipData.newIntent("Intent",appIntent);
Put the new clip object on the clipboard:
// Set the clipboard's primary clip.
clipboard.setPrimaryClip(clip);
// Gets the ID of the "paste" menu item
MenuItem mPasteItem = menu.findItem(R.id.menu_paste);


// If the clipboard doesn't contain data, disable the paste menu item.
// If it does contain data, decide if you can handle the data.
if (!(clipboard.hasPrimaryClip())) {


    mPasteItem.setEnabled(false);


    } else if (!(clipboard.getPrimaryClipDescription().hasMimeType(MIMETYPE_TEXT_PLAIN))) {


        // This disables the paste menu item, since the clipboard has data but it is not plain text
        mPasteItem.setEnabled(false);
    } else {


        // This enables the paste menu item, since the clipboard contains plain text.
        mPasteItem.setEnabled(true);
    }
}
》To set up your application to copy a data stream with a provider, follow these steps:
1.Set up a content URI for the data stream you are putting on the clipboard. 
2.Provide a MIME type for each type of data stream you plan to offer. Pasting applications need this information to determine if they can paste the data on the clipboard.
3.Implement one of the ContentProvider methods that returns a file descriptor for a stream. If you encode identifiers on the content URI, use this method to determine which stream to open.
4.To copy the data stream to the clipboard, construct the content URI and place it on the clipboard.
》 To design effective copy and paste functionality for your application, remember these points:
At any time, there is only one clip on the clipboard. A new copy operation by any application in the system overwrites the previous clip. Since the user may navigate away from your application and do a copy before returning, you can't assume that the clipboard contains the clip that the user previously copied in yourapplication.
The intended purpose of multiple ClipData.Item objects per clip is to support copying and pasting of multiple selections rather than different forms of reference to a single selection. You usually want all of theClipData.Item objects in a clip to have the same form, that is, they should all be simple text, content URI, orIntent, but not a mixture.
When you provide data, you can offer different MIME representations. Add the MIME types you support to theClipDescription, and then implement the MIME types in your content provider.
When you get data from the clipboard, your application is responsible for checking the available MIME types and then deciding which one, if any, to use. Even if there is a clip on the clipboard and the user requests a paste, your application is not required to do the paste. You should do the paste if the MIME type is compatible. You may choose to coerce the data on the clipboard to text using coerceToText() if you choose. If your application supports more than one of the available MIME types, you can allow the user to choose which one to use.
0 0
原创粉丝点击