How can I copy Section of textView not all of that in this code?

扶醉桌前 提交于 2019-12-13 04:25:30

问题


I have this code from Mr Naddy for copying textView to clipboard in API 7

TextView textView=(TextView)findViewById(R.id.textView1);
registerForContextMenu(textView);

Then override onCreateContextMenu -

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    //user has long pressed your TextView
    menu.add(0, v.getId(), 0, "Copy");

    //cast the received View to TextView so that you can get its text
    TextView textView = (TextView) v;

    //place your TextView's text in clipboard
    ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); 
    clipboard.setText(textView.getText());
}

It works ..... but the User can copy just all of textview ................. I want allow to user if he wants he can copy section of textview .... maybe user want to copy for example some words not all of textview ???????

what should I do ??


回答1:


have you tried this ?

TextView tv;
String stringYouExtracted = tv.getText().toString;
int startIndex = tv.getSelectionStart();
int endIndex = tv.getSelectionEnd();
stringYouExtracted = stringYouExtracted.subString(startIndex, endIndex);
ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
clipboard.setText(stringYouExtracted);

found at: Android: Copy to clipboard selected text from a TextView



来源:https://stackoverflow.com/questions/22742352/how-can-i-copy-section-of-textview-not-all-of-that-in-this-code

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!