单击菜单后如何显示吐司?
How to show toast after menu is clicked?
我有一个 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());
}
我想在点击菜单后显示 toast "copied to clipboard",我该怎么做?我的第二个问题是为什么不推荐使用 ClipManager?用什么代替?
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;
}
}
对于 SDK>=15,这将有效
ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("label for text", "text to copy");
clipboard.setPrimaryClip(clip);
我有一个 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());
}
我想在点击菜单后显示 toast "copied to clipboard",我该怎么做?我的第二个问题是为什么不推荐使用 ClipManager?用什么代替?
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;
}
}
对于 SDK>=15,这将有效
ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("label for text", "text to copy");
clipboard.setPrimaryClip(clip);