2014-03-30 43 views
0

我從Naddy先生這段代碼拷貝的TextView在API剪貼板7如何在此代碼中複製textView的所有部分?

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

然後覆蓋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()); 
} 

它的工作原理.....但用戶可以複製所有的textview .................我想允許用戶,如果他想要他可以複製文本視圖的部分....也許用戶想複製一些文字不是所有的textview? ??????

我該怎麼辦?

回答

1

你試過嗎?

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); 

發現在:Android: Copy to clipboard selected text from a TextView

+0

我應該在onCreate方法增加您的代碼.... ??? – user3410344

+0

當我運行它.....「不幸的是,copy2已停止」出現........爲什麼? – user3410344

+0

時,你會點擊上面的鏈接,你會發現你需要測試它的Android版本 –