我有一个textView,我将其文本可复制,然后覆盖onCreateContextMenu:
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
//user has long pressed your TextView
menu.add(0, v.getId(), 0, "Copy");
TextView yourTextView = (TextView) v;
ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
clipboard.setText(yourTextView.getText());
}
我想要展示吐司"复制到剪贴板"单击菜单后,我该怎么办?我的第二个问题是为什么不推荐使用ClipManager?用什么呢?
答案 0 :(得分:1)
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
//user has long pressed your TextView
menu.add(0, v.getId(), 0, "Copy");
TextView yourTextView = (TextView) v;
ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
clipboard.setText(yourTextView.getText());
}
@Override
public boolean onContextItemSelected(MenuItem item) {
super.onContextItemSelected(item);
if (item.getTitle() == "Copy") {
Toast.makeText(getApplicationContext(), "copied to clipboard", Toast.LENGTH_LONG).show();
}
return true;
}
}
for SDK> = 15,这将有效
ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("label for text", "text to copy");
clipboard.setPrimaryClip(clip);