Доброго времени суток.
Я хочу сделать, чтобы по нажатию на кнопку содержимое TextView
копировалось в буфер.
Button copyButton = (Button)findViewById(R.id.copyButton);
TextView generatedCode = (TextView) findViewById(R.id.generatedCode);
String stringYouExtracted = generatedCode.getText().toString();
int startIndex = generatedCode.getSelectionStart();
int endIndex = generatedCode.getSelectionEnd();
stringYouExtracted = (stringYouExtracted).substring(startIndex, endIndex);
if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB) {
android.text.ClipboardManager clipboard = (android.text.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
clipboard.setText(stringYouExtracted);
} else {
android.content.ClipboardManager clipboard = (android.content.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
android.content.ClipData clip = android.content.ClipData.newPlainText("Copied Text", stringYouExtracted);
clipboard.setPrimaryClip(clip);
}
Данный код работает некорректно из-за строчки:
stringYouExtracted = (stringYouExtracted).substring(startIndex, endIndex);
кидает ClassCastException
.
Заранее спасибо.