<?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" > <EditText android:id="@+id/editcont" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" > <requestFocus /> </EditText> <Button android:id="@+id/setclipboard" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="设置剪贴板内容" /> <Button android:id="@+id/getclipboard" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="获取剪贴板内容" /> </LinearLayout>
package com.example.yanlei.wifi; import android.content.ClipboardManager; import android.content.Context; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private EditText editCont = null; private Button btnSet = null; private Button btnGet = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnSet = (Button) super.findViewById(R.id.setclipboard); btnGet = (Button) super.findViewById(R.id.getclipboard); editCont = (EditText) super.findViewById(R.id.editcont); //将文本框中内容放入剪贴板 btnSet.setOnClickListener(new OnClickListener() { public void onClick(View v) { String info = editCont.getText().toString(); //获取剪贴板管理服务 ClipboardManager clipboard = (ClipboardManager) MainActivity.this.getSystemService(Context.CLIPBOARD_SERVICE); //将文本数据复制到剪贴板 clipboard.setText(info); } }); //把剪贴板内容使用Toast显示出来 btnGet.setOnClickListener(new OnClickListener() { public void onClick(View v) { String info = ""; //获取剪贴板管理服务 ClipboardManager clipboard = (ClipboardManager) MainActivity.this.getSystemService(Context.CLIPBOARD_SERVICE); //从剪贴板读取内容 info = clipboard.getText().toString(); Toast.makeText(getApplicationContext(), info, Toast.LENGTH_LONG).show(); } }); } }
原文:http://www.cnblogs.com/gisoracle/p/5224790.html