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 );