<span id="Label3"></p><span style="font-size: 18pt; color: #3366ff;">the <span style="font-size: 18pt; color: #3366ff;">Clipboard Framework</span></span><p><p><span style="font-size: 15px;">When using the Clipboard framework, The data is placed in a cut object (clip Object) and the object is placed in the clipboard of the System.</span></p></p><p><p><span style="font-size: 15px;"> <span><strong>The clip object can be in three different forms:</strong></span></span></p></p><p><p><span style="font-size: 15px;"> <span style="color: #ff0000;"><strong>Text: literal String.</strong></span></span></p></p><p><p><span style="font-size: 15px;">The text is placed directly in the clip object and then placed in the clipboard; when you paste the string, you get the object directly from the Clipboard and put the string into your app Store.</span></p></p><p><p><span style="font-size: 15px;"> <span style="color: #ff0000;"><strong>URI: an <code><span style="color: #ff0000;">Uri</span></code> Object.</strong></span></span></p></p><p><p><span style="font-size: 15px;">Represents any form of Uri. This form is primarily used to replicate complex data from a content provider.</span></p></p><p><p><span style="font-size: 15px;">When copying, place an <code>Uri</code> object in a clip object and then put it on the clipboard, remove the clip object when pasting, get the uri, parse it into a data resource such as content provider, and then copy the data from the resource to the application Store.</span></p></p><p><p><span style="font-size: 15px;"> <span style="color: #ff0000;"><strong>Intent: <code><span style="color: #ff0000;">Intent</span></code> Object.</strong></span></span></p></p><p><p><span style="font-size: 15px;">This supports the Replication app Shortcut.</span></p></p><p><p><span style="font-size: 15px;">When copying, place the intent object in the clip object, put it into the clipboard, and when you paste the data, get the intent object from the clip object and put it in the app storage Area.</span></p></p><p><p></p></p><p><p><span style="font-size: 15px;"> <span style="color: #ff0000;"><strong>The Clipboard only holds one clip object at a time, and the previous one disappears when the app puts another clip object in.</strong></span></span></p></p><span style="font-size: 18pt; color: #3366ff;"><span style="font-size: 18pt; color: #3366ff;">the Clipboard class</span></span><p><p><span><strong><span style="font-size: 14pt;">Clipboardmanager</span></strong></span></p></p><p><p><span style="font-size: 15px;"><strong>Clipboardmanager</strong> represents the clipboard of the system, obtained through <strong>getsystemservice (clipboard_service)</strong> .</span></p></p><p><p><span style="font-size: 15px;">The full name of <span style="color: #ff0000;"> <strong>Android.text.ClipboardManager</strong> is deprecated starting with API 11.</span></span></p></p><p><p><span style="font-size: 15px;">It is replaced by its subclass:<span style="color: #ff0000;"><strong>android.content.ClipboardManager</strong> (since API level 11).</span></span></p></p><p><p></p></p><p><p><span><strong><span style="font-size: 14pt;">clipdata, clipdescription, and Clipdata.item</span></strong></span></p></p><p><p><span style="font-size: 15px;">The clip object that was said earlier is the object of the <strong>clipdata</strong> class, <strong></strong> which contains an object <code><strong>ClipDescription</strong></code> and one or more <code><strong>ClipData.Item</strong></code> Objects.</span></p></p><p><p><span style="font-size: 15px;">the <strong>clipdescription</strong> object contains an array that describes the MIME type of the clip Object.</span></p></p><p><p><span style="font-size: 15px;">the <strong>Clipdata.item</strong> object contains text, uri, or intent Data. <strong>a Clip object can contain one or more item objects</strong> .</span></p></p><p><p></p></p><p><p><span style="font-size: 15px;">For example, if you want to copy multiple items from a list, you can create a <strong>clipdata.item</strong> object for each item in the list, and then put them into a <strong>clipdata</strong> object, so that you put multiple data on the Clipboard at Once.</span></p></p><p><p><span style="font-size: 15px;">Note that the Clipdata class is API 11 only.</span></p></p><p><p></p></p><p><p><span><strong><span style="font-size: 14pt;">A concise method in Clipdata</span></strong></span></p></p><p><p><span style="font-size: 15px;">There are static and concise methods in the <strong>Clipdata</strong> class for creating Clipdata objects with only one <strong>Clipdata.item</strong> and one<strong> </strong>simple description ( <code><strong>ClipDescription</strong></code> ).</span></p></p><p><p><span style="font-size: 15px;"><strong>newplaintext (label, Text)</strong> returns the Clipdata object, the data is literal text, and the description is Label,mime type is <code>MIMETYPE_TEXT_PLAIN</code> .</span></p></p><p><p><span style="font-size: 15px;">This is similar to:</span></p></p><p><p><span style="font-size: 15px;"><strong>Newuri (resolver, label, URI)</strong></span></p></p><p><p><span style="font-size: 15px;"><strong>Newintent (label, Intent)</strong></span></p></p><p><p><span style="font-size: 15px;"></span></p></p><p><p><span style="font-size: 15px;"></span></p></p><p><p><span><strong><span style="font-size: 14pt;">Make the data in the Clipboard strong to text</span></strong></span></p></p><p><p><span style="font-size: 15px;">Non-text data in the Clipboard can be converted to text processing through the <strong>ClipData.Item.coerceToText ()</strong> method.</span></p></p><p><p><span style="font-size: 15px;">1. This method first detects whether the item contains text, and if so, returns DIRECTLY.</span></p></p><p><p><span style="font-size: 15px;">2. does not contain text, then see if there is a uri:</span></p></p><p><p><span style="font-size: 15px;">If the URI is a content uri, and provider returns a text stream, Coercetotext () returns the text stream;</span></p></p><p><p><span style="font-size: 15px;">If provider does not return a text stream, or if the URI is not the content Uri,coercetotext () method, It returns the expression of the uri, that is <code>Uri.toString()</code> .</span></p></p><p><p><span style="font-size: 15px;">3. finally, If the item does not contain text and does not have a uri, then it should contain intent, which <code>coerceToText()</code> translates the intent object into a intent URI return, and the <code>Intent.toUri(URI_INTENT_SCHEME)</code> Same.</span></p></p><p><p></p></p><p><p><span><strong><span style="font-size: 14pt;">Android Clipboard Framework Summary</span></strong></span></p></p><p><p><span style="font-size: 15px;"> </span></p></p><p><p><span style="font-size: 15px;"></span></p></p><p><p><span style="font-size: 15px;"></span></p></p><p><p><span style="font-size: 15px;"></span></p></p><span style="font-size: 18pt; color: #3366ff;"><span style="font-size: 18pt; color: #3366ff;">Copy to Clipboard</span></span><p><p><span style="font-size: 15px; line-height: 1.5;"> <strong>1. first, Get the Clipboard service:</strong></span></p></p><pre><pre><span style="color: #008000;"><span style="color: #008000;">Gets a handle to the Clipboard service. Clipboardmanager Clipboard =<span style="color: #000000;"> (clipboardmanager) getsystemservice (context.clipboard_service);</span> </span></span></pre></pre><p><p></p></p><p><p><span style="font-size: 15px;"> <strong>2. Then put the data in the Clipdata Object.</strong></span></p></p><p><p><span style="font-size: 15px;">To Text:</span></p></p><pre><pre><span style="color: #008000;"><span style="color: #008000;">creates a new text clip to put in the clipboardclipdata clip = Clipdata.newplaintext ("simple text", "Hello, world!" );</span></span></pre></pre><p><p><span style="font-size: 15px;">For Uri:</span></p></p><pre><span style="color: #008000;">//<span style="color: #008000;">Creates a URI based on a base URI and a record ID based on the Contact's last name<span style="color: #008000;">//<span style="color: #008000;">Declares the base URI string<span style="color: #0000ff;">Private<span style="color: #0000ff;">Static<span style="color: #0000ff;">Final String CONTACTS = "content://com.example.contacts"<span style="color: #000000;">;<span style="color: #008000;">//<span style="color: #008000;">Declares a path string for URIs the use to copy data<span style="color: #0000ff;"><span style="color: #0000ff;">private <span style="color: #0000ff;">static <span style="color: #0000ff;">final String Copy_path = "/copy"<span style="color: #000000;">; <span style="color: #008000;"> <span style="color: #008000;">declares the Uri to paste to the Clipboarduri Copyuri = uri.parse (CONTACTS + copy_path + "/" +<span style="color: #000000;"> lastName); .... <c7> // <span style="color: #008000;"> creates a new URI clip Object. The system uses the anonymous Getcontentresolver () object to<span style="color: #008000;">//<span style="color: #008000;"> get MIME types from Provider. The Clip Object's label is a "uri", and its data is<span style="color: #008000;">//the<span style="color: #008000;"> URI previously Created. Clipdata clip = Clipdata.newuri (getcontentresolver (), "URI", copyuri);</span> </span> </span> </span> </span> </c7></span> </span> </span> </span> </span></span></span></span></span></span></span></span></span></span></span></span></span></span></pre><p><p><span style="font-size: 15px;">For Intent:</span></p></p> <pre><pre><span style="color: #008000;"><span style="color: #008000;"> <span style="color: #0000ff;">New Intent (this<span style="color: #0000ff;">, Com.example.demo.myapplication. <span style="color: #0000ff;"> Class<span style="color: #000000;">), ..... <span style="color: #008000;">//<span style="color: #008000;"> Creates a clip object with the Intent in it. Its label was "Intent" and its data are<span style="color: #008000;">//the<span style="color: #008000;"> Intent object created previouslyclipdata clip = Clipdata.newintent ("in Tent ", appintent);</span> </span> </span> </span> </span> </span> </span> </span></span></span></pre></pre><p><p></p></p><p><p><span style="font-size: 15px;"> <strong>3. Place the Clip object on the Clipboard:</strong></span></p></p><pre><pre><span style="color: #008000;"><span style="color: #008000;">Set the Clipboard ' s primary clip.clipboard.setPrimaryClip (clip);</span></span></pre></pre><pre><pre><span style="font-size: 15px;"></span></pre></pre><span style="font-size: 18pt; color: #3366ff;"><span style="font-size: 18pt; color: #3366ff;">pasting from the Clipboard</span></span><p><p><span style="font-size: 15px;">Take the text paste as an example, others slightly.</span></p></p><p><p><span style="font-size: 15px;">Examples are as Follows:</span></p></p><pre><span style="color: #0000ff;">Package<span style="color: #000000;">com.example.helloclipboard;<span style="color: #0000ff;">Import<span style="color: #000000;">android.os.Bundle;<span style="color: #0000ff;">Import<span style="color: #000000;">android.app.Activity;<span style="color: #0000ff;">Import<span style="color: #000000;">android.content.ClipData;<span style="color: #0000ff;">Import<span style="color: #000000;">android.content.ClipboardManager;<span style="color: #0000ff;">Import<span style="color: #000000;">android.content.Context;<span style="color: #0000ff;">Import<span style="color: #000000;">android.util.Log;<span style="color: #0000ff;">Import<span style="color: #000000;">android.view.View;<span style="color: #0000ff;">Import<span style="color: #000000;">android.view.View.OnClickListener;<span style="color: #0000ff;">Import<span style="color: #000000;">android.widget.Button;<span style="color: #0000ff;">Import<span style="color: #000000;">android.widget.EditText;<span style="color: #0000ff;">Import<span style="color: #000000;">android.widget.TextView;<span style="color: #0000ff;">Import<span style="color: #000000;">android.widget.Toast;<span style="color: #0000ff;">Public<span style="color: #0000ff;">Class Helloclipboardmainactivity<span style="color: #0000ff;">Extends<span style="color: #000000;">Activity {<span style="color: #0000ff;">Private EditText MEditText1 =<span style="color: #0000ff;">Null<span style="color: #000000;">;<span style="color: #0000ff;">Private TextView Mresulttextview =<span style="color: #0000ff;">Null<span style="color: #000000;">;<span style="color: #0000ff;">Private Clipboardmanager Mclipboard =<span style="color: #0000ff;">Null<span style="color: #000000;">; @Override<span style="color: #0000ff;">Protected<span style="color: #0000ff;">void<span style="color: #000000;">OnCreate (Bundle Savedinstancestate) {<span style="color: #0000ff;">Super<span style="color: #000000;">. onCreate (savedinstancestate); Setcontentview (r.layout.activity_hello_clipboard_main); Mresulttextview =<span style="color: #000000;">(TextView) Findviewbyid (r.id.textview1); MEditText1 =<span style="color: #000000;">(EditText) Findviewbyid (r.id.edittext1); Button Copybutton =<span style="color: #000000;">(Button) Findviewbyid (r.id.button1); Button Pastebutton =<span style="color: #000000;">(Button) Findviewbyid (r.id.button2); Copybutton.setonclicklistener (monclicklistener); Pastebutton.setonclicklistener (monclicklistener); }<span style="color: #0000ff;">Private Onclicklistener Monclicklistener =<span style="color: #0000ff;">New<span style="color: #000000;">Onclicklistener () {@Override<span style="color: #0000ff;">Public<span style="color: #0000ff;">void<span style="color: #000000;">OnClick (View V) {<span style="color: #0000ff;">Switch<span style="color: #000000;">(v.getid ()) {<span style="color: #0000ff;">Case<span style="color: #000000;">R.id.button1:copyfromedittext1 ();<span style="color: #0000ff;">Break<span style="color: #000000;">;<span style="color: #0000ff;">Case<span style="color: #000000;">R.id.button2:pastetoresult ();<span style="color: #0000ff;">Break<span style="color: #000000;">;<span style="color: #0000ff;">Default<span style="color: #000000;">:<span style="color: #0000ff;">Break<span style="color: #000000;">; } } };<span style="color: #0000ff;">Private<span style="color: #0000ff;">void<span style="color: #000000;">CopyFromEditText1 () {<span style="color: #008000;">//<span style="color: #008000;">Gets a handle to the Clipboard service.<span style="color: #0000ff;">if (<span style="color: #0000ff;">NULL = =<span style="color: #000000;">Mclipboard) {mclipboard =<span style="color: #000000;">(clipboardmanager) Getsystemservice (context.clipboard_service); }<span style="color: #008000;">//<span style="color: #008000;">Creates a new text clip to put in the Clipboard clipdata clip = Clipdata.newplaintext ("simple text"<span style="color: #000000;">, Medittext1.gettext ());<span style="color: #008000;">//<span style="color: #008000;">Set the Clipboard ' s primary clip.<span style="color: #000000;">Mclipboard.setprimaryclip (clip); }<span style="color: #0000ff;">Private<span style="color: #0000ff;">void<span style="color: #000000;">Pastetoresult () {<span style="color: #008000;">//<span style="color: #008000;">Gets a handle to the Clipboard service.<span style="color: #0000ff;">if (<span style="color: #0000ff;">NULL = =<span style="color: #000000;">Mclipboard) {mclipboard =<span style="color: #000000;">(clipboardmanager) Getsystemservice (context.clipboard_service); } String resultstring = ""<span style="color: #000000;">;<span style="color: #008000;">//<span style="color: #008000;">Check if the Clipboard has content<span style="color: #0000ff;">If (!<span style="color: #000000;">Mclipboard.hasprimaryclip ()) {toast.maketext (helloclipboardmainactivity.<span style="color: #0000ff;">This<span style="color: #000000;">, "Clipboard is empty"<span style="color: #000000;"><span style="color: #000000;">, toast.length_short). show (); } <span style="color: #0000ff;">Else<span style="color: #000000;"> {clipdata clipdata =<span style="color: #000000;"> mclipboard.getprimaryclip (); <span style="color: #0000ff;">int count =<span style="color: #000000;"> Clipdata.getitemcount (); for <span style="color: #0000ff;">(<span style="color: #0000ff;">int i = 0; I < count; + +<span style="color: #000000;">i) {clipdata.item Item =<span style="color: #000000;"> Clipdata.getitemat (i); Charsequence str =<span style="color: #000000;"> item. coercetotext (helloclipboardmainactivity. <span style="color: #0000ff;"> This<span style="color: #000000;">); LOG.I ("mengdd", "item:" + i + ":" +<span style="color: #000000;"> str); Resultstring + =<span style="color: #000000;"> str;}} Mresulttextview.settext (resultstring); }}</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></pre><p><p></p></p><p><p></p></p><span style="font-size: 18pt; color: #3366ff;"><span style="font-size: 18pt; color: #3366ff;">Design effective Copy-and-paste functionality</span></span><p><p><span style="font-size: 15px;">In order to design an effective copy and paste function, the following points need to be noted:</span></p></p><p><p><span style="font-size: 15px;">1. At any time, there is only one clip object in the Clipboard.</span></p></p><p><p><span style="font-size: 15px;">The new copy will overwrite the previous clip object because the user may exit from your app and copy something from another app, so you can't assume that the last thing a user has copied in your app must still be on the Clipboard.</span></p></p><p><p></p></p><p><p><span style="font-size: 15px;">2. A clip object, which is multiple objects in clipdata, <code>ClipData.Item</code> is designed to support multiple options for copy-and-paste, rather than to support multiple forms of a single radio.</span></p></p><p><p><span style="font-size: 15px;">You usually need all the items in the clip object, that is, Clipdata.item have the same form, such as text, which are either URIs or intent, rather than mixed in various forms.</span></p></p><p><p></p></p><p><p><span style="font-size: 15px;">3. When you provide data, you can provide different MIME expressions.</span></p></p><p><p><span style="font-size: 15px;">Add the MIME type you support to <strong>clipdescription</strong> and implement it in your content provider.</span></p></p><p><p></p></p><p><p><span style="font-size: 15px;">4. When you get data from the clipboard, your app is responsible for checking the available MIME types and then deciding which one to Use.</span></p></p><p><p><span style="font-size: 15px;">Even if you have a clip object on the Clipboard and the user asks to paste it, your app may not need to Paste.</span></p></p><p><p><span style="font-size: 15px;">You should perform the paste operation when the MIME type is Compatible. You can choose to use the <code><strong>coerceToText()</strong></code> method to convert the pasted content to Text.</span></p></p><p><p><span style="font-size: 15px;">If your app supports multiple types, you can let the user choose which one to Use.</span></p></p><p><p><span style="font-size: 15px;"></span></p></p><span style="font-size: 18pt; color: #3366ff;"><span style="font-size: 18pt; color: #3366ff;">References</span></span><p><p><span style="font-size: 15px;">API Guides:copy and Paste:</span></p></p><p><p><span style="font-size: 15px;">Http://developer.android.com/guide/topics/text/copy-paste.html</span></p></p><p><p>Copy and paste in Android</p></p></span>
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