问题
I need to have a text that will be shown to the user, and it will be possible to select a part of it and copy it to the clipboard. But without the keyboard open. I know that on api 11 and newer i can just use
text.setTextIsSelectable (true);
But what is the solution to lower OS versions?
回答1:
Use ContextMenu
and CLIPBOARD_SERVICE
:
private TextView mTextView;
protected final void onCreate(Bundle savedInstanceState) {
...
registerForContextMenu(mTextView);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo) {
TextView textView = (TextView) view;
menu.setHeaderTitle(textView.getText()).add(0, 0, 0, R.string.menu_copy_to_clipboard);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
((ClipboardManager) getSystemService(CLIPBOARD_SERVICE)).setText(mTextView.getText());
return true;
}
回答2:
After a long and time consuming search, I can't find a component that can select text in textview for android API level <=11. I have written this component that may be of help to you : new Selectable TextView in android 3 (API <=11) component
来源:https://stackoverflow.com/questions/16167330/how-to-make-textview-selectable-under-api-11