The simplest practice of copying text content from Android to the system clipboard: android clipboard

Source: Internet
Author: User

The simplest practice of copying text content from Android to the system clipboard: android clipboard

This example is very simple.

The layout file activity_copy.xml code is as follows:

<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical"> <TextView android: id = "@ + id/tvMsg" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: text = "a reporter asked everyone:" aunt, do you think smog has a big impact?" Passers-by: "Isn't it big? First of all, you need to know that I am your uncle. "Android: textSize =" 20sp "/> <Button android: layout_width =" fill_parent "android: layout_height =" wrap_content "android: layout_marginTop =" 20dp "android: onClick = "onClickCopy" android: text = "copy the text above"/> </LinearLayout>

The CopyActivity. java code in the background is as follows:

Package chengyujia. demo. aty; import android. content. context; import android. OS. bundle; import android. text. clipboardManager; import android. view. view; import android. widget. textView; import android. widget. toast; import chengyujia. demo. r; public class CopyActivity extends BaseActivity {private TextView tvMsg; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState ); SetContentView (R. layout. activity_copy); tvMsg = (TextView) findViewById (R. id. tvMsg);} public void onClickCopy (View v) {// android is recommended from API11. content. clipboardManager // for compatibility with earlier versions, we use the old android version here. text. clipboardManager, although the prompt deprecated, does not affect the use. ClipboardManager cm = (ClipboardManager) getSystemService (Context. CLIPBOARD_SERVICE); // place the text content to the system clipboard. Cm. setText (tvMsg. getText (); Toast. makeText (this, "Copied successfully. You can send it to friends. ", Toast. LENGTH_LONG). show ();}}

The core code is as follows:

ClipboardManager cm = (ClipboardManager) getSystemService (Context. CLIPBOARD_SERVICE );

Cm. setText (text content to be copied );

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.