Crazy Brush Android Example two: clipboard example

Source: Internet
Author: User

Crazy Brush Android Example two: clipboard example clipboardsample description

The Mad Brush Android sample series has opened. Each learning an Android paradigm, a single example generates a running app and a brief analysis of key source code. Then provide the packaged source code download.

Function

The functionality is simple, using the Clipboard provided by Android, copying different types of data to the Clipboard.
The code package is here, without the download points:
http://download.csdn.net/detail/logicteamleader/8786187

Source

The clipboardsample example comes from the com.example.android.apis.content.ClipboardSample of Android-20.

Environment

Code Runtime Environment:
1.adt2014 version;
2.android:minsdkversion= "+"; android:targetsdkversion= "20"
The APPCOMPATV7 has been generated in 3.workspace, and its version is android-22;

Code

Note Several key classes and methods: Clipboard manager Clipboardmanager, slice data class Clipdata, array adapter class Arrayadapter,getsystemservice (Clipboard_service) method.
Other content research code can be.

/** * Copyright (c), the Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "Lic Ense "); * You are not a use this file except in compliance with the License. * Obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * unless required by applic Able law or agreed to in writing, software * Distributed under the License are distributed on a "as is" BASIS, * without W Arranties or CONDITIONS of any KIND, either express OR implied. * See the License for the specific language governing permissions and * limitations under the License. */ PackageCom.example.clipboardsample;Importandroid.app.Activity;ImportAndroid.content.ClipData;ImportAndroid.content.ClipboardManager;ImportAndroid.content.Intent;ImportAndroid.net.Uri;ImportAndroid.os.Bundle;ImportAndroid.text.method.LinkMovementMethod;ImportAndroid.view.View;ImportAndroid.widget.AdapterView;ImportAndroid.widget.AdapterView.OnItemSelectedListener;ImportAndroid.widget.ArrayAdapter;ImportAndroid.widget.Spinner;ImportAndroid.widget.TextView; Public  class clipboardsample extends Activity {Clipboardmanager Mclipboard;    Spinner Mspinner;    TextView mmimetypes;    TextView Mdatatext;    Charsequence Mstyledtext;    String Mplaintext;    String Mhtmltext;    String Mhtmlplaintext; Clipboardmanager.onprimaryclipchangedlistener Mprimarychangelistener =NewClipboardmanager.onprimaryclipchangedlistener () { Public void onprimaryclipchanged() {Updateclipdata (true); }    };@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);//Get the Clipboard Manager via Getsystemservice (Clipboard_service)Mclipboard = (Clipboardmanager) getsystemservice (Clipboard_service);//See res/any/layout/resources.xml for this view layout definition.Setcontentview (R.layout.clipboard);        TextView TV;        Mstyledtext = GetText (R.string.styled_text);        TV = (TextView) Findviewbyid (R.id.styled_text);        Tv.settext (Mstyledtext);        Mplaintext = Mstyledtext.tostring ();        TV = (TextView) Findviewbyid (R.id.plain_text);        Tv.settext (Mplaintext); Mhtmltext ="<b>Link:</b> <a href=\" http://www.android.com\ ">Android</a>"; Mhtmlplaintext ="Link:http://www.android.com";        TV = (TextView) Findviewbyid (R.id.html_text);        Tv.settext (Mhtmltext); Mspinner = (Spinner) Findviewbyid (R.id.clip_type);//These three lines of code create a classic arrayadapter used by spinner, which uses a resource file R.array.clip_data_typesarrayadapter<charsequence> adapter = Arrayadapter.createfromresource ( This, R.array.clip_data_types, Android.        R.layout.simple_spinner_item); Adapter.setdropdownviewresource (Android.        R.layout.simple_spinner_dropdown_item);        Mspinner.setadapter (adapter); Mspinner.setonitemselectedlistener (NewOnitemselectedlistener () { Public void onitemselected(adapterview<?> parent, view view,intPositionLongID) {Updateclipdata (false); } Public void onnothingselected(adapterview<?> Parent)        {                    }                });        Mmimetypes = (TextView) Findviewbyid (r.id.clip_mime_types);        Mdatatext = (TextView) Findviewbyid (R.id.clip_text);        Mclipboard.addprimaryclipchangedlistener (Mprimarychangelistener); Updateclipdata (true); }@Override    protected void OnDestroy() {Super. OnDestroy ();    Mclipboard.removeprimaryclipchangedlistener (Mprimarychangelistener); } Public void Pastestyledtext(View button) {Mclipboard.setprimaryclip (Clipdata.newplaintext ("Styled Text", Mstyledtext)); } Public void Pasteplaintext(View button) {Mclipboard.setprimaryclip (Clipdata.newplaintext ("Styled Text", Mplaintext)); } Public void Pastehtmltext(View button) {Mclipboard.setprimaryclip (Clipdata.newhtmltext ("HTML Text", Mhtmlplaintext, Mhtmltext)); } Public void pasteintent(View button) {Intent Intent =NewIntent (Intent.action_view, Uri.parse ("http://www.android.com/")); Mclipboard.setprimaryclip (Clipdata.newintent ("VIEW Intent", intent)); } Public void Pasteuri(View button) {Mclipboard.setprimaryclip (Clipdata.newrawuri ("URI", Uri.parse ("http://www.android.com/"))); }//function to update clipboard contents    voidUpdateclipdata (BooleanUpdatetype) {Clipdata clip = Mclipboard.getprimaryclip (); string[] Mimetypes = clip! =NULL? Clip.getdescription (). Filtermimetypes ("*/*") :NULL;if(Mimetypes! =NULL) {Mmimetypes.settext (""); for(intI=0; i<mimetypes.length; i++) {if(I >0) {Mmimetypes.append ("\ n");            } mmimetypes.append (Mimetypes[i]); }        }Else{Mmimetypes.settext ("NULL"); }if(Updatetype) {if(Clip! =NULL) {Clipdata.item Item = Clip.getitemat (0);if(Item.gethtmltext ()! =NULL) {Mspinner.setselection (2); }Else if(Item.gettext ()! =NULL) {Mspinner.setselection (1); }Else if(Item.getintent ()! =NULL) {Mspinner.setselection (3); }Else if(Item.geturi ()! =NULL) {Mspinner.setselection (4); }Else{Mspinner.setselection (0); }            }Else{Mspinner.setselection (0); }        }if(Clip! =NULL) {Clipdata.item Item = Clip.getitemat (0);Switch(Mspinner.getselecteditemposition ()) { Case 0: Mdatatext.settext ("(No data)"); Break; Case 1: Mdatatext.settext (Item.gettext ()); Break; Case 2: Mdatatext.settext (Item.gethtmltext ()); Break; Case 3: Mdatatext.settext (Item.getintent (). Touri (0)); Break; Case 4: Mdatatext.settext (Item.geturi (). toString ()); Break; Case 5: Mdatatext.settext (Item.coercetotext ( This)); Break; Case 6: Mdatatext.settext (Item.coercetostyledtext ( This)); Break; Case 7: Mdatatext.settext (Item.coercetohtmltext ( This)); Break;default: Mdatatext.settext ("Unknown option:"+ mspinner.getselecteditemposition ()); Break; }        }Else{Mdatatext.settext ("(NULL clip)");    } mdatatext.setmovementmethod (Linkmovementmethod.getinstance ()); }}

Crazy Brush Android Example two: clipboard example

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.