public class MainActivity extends ListActivity {
private EditText tv_info;
private ClipboardManager cm;//剪贴板管理器
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] array = { "复制文本-废弃", "获取复制的文本-废弃", "复制内容-新API", "获取文本内容", "获取URI", "获取Intent", };
tv_info = new EditText(this);// 将内容显示在TextView中
tv_info.setTextColor(Color.RED);
tv_info.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
tv_info.setPadding(20, 40, 20, 40);
getListView().addFooterView(tv_info);
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, new ArrayList<String>(Arrays.asList(array))));
cm = (android.content.ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
}
@SuppressWarnings("deprecation")
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
switch (position) {
case 0:
cm.setText(tv_info.getText().toString().trim());
break;
case 1:
Toast.makeText(this, cm.getText().toString().trim(), Toast.LENGTH_SHORT).show();
break;
case 2:
//创建能够存入剪贴板的ClipData对象
ClipData mClipData = ClipData.newPlainText("label", "包青天");//创建纯文本字符型ClipData,支持5种形式
mClipData.addItem(new ClipData.Item(tv_info.getText().toString().trim()));//添加纯文本
mClipData.addItem(new ClipData.Item(Uri.parse("http://www.baidu.cn/")));//添加URL型ClipData
mClipData.addItem(new ClipData.Item(new Intent(Settings.ACTION_SETTINGS)));//添加Intent型ClipData
cm.setPrimaryClip(mClipData);
break;
case 3:
//获取存入剪贴板的ClipData对象,要自己保证获取到的数据类型是正确的类型
ClipData data = cm.getPrimaryClip();
tv_info.setText("ClipData.Item数量为:" + data.getItemCount() + "\n" + data.getItemAt(0).getText() + "\n" + data.getItemAt(1).getText());
break;
case 4:
Uri uri = cm.getPrimaryClip().getItemAt(2).getUri();
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
break;
case 5:
startActivity(cm.getPrimaryClip().getItemAt(3).getIntent());
break;
}
}
}