Android:将 TextView 中选定的文本复制到剪贴板

发布于 2024-11-19 05:49:39 字数 115 浏览 3 评论 0原文

是否可以仅将选定的文本从 TextView UI 组件复制到剪贴板?

我已经捕获了长按事件,并将全文复制到剪贴板,但现在我想指定要从 TextView 复制的选择的开始和结束。

谢谢。

Is there a possibility to copy to clipboard from a TextView UI component only the selected text?

I've catched the long press event and I copied the full text to clipboard, but now I want to specify the start and the end of the selection to be copied from a TextView.

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如此安好 2024-11-26 05:49:39
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);

编辑(前面是完整的答案,但我错误地找到了答案,所以我想添加)

使用较新的 API,将最后两行更改为:

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

“Copied Text” 是较新 APIS 中 COPY 实体的标题

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

EDIT (The previous is the full answer, but I ran into my answer by mistake so I would like to add):

With Newer APIs, change the last two lines to :

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

"Copied Text" is a title for your COPY entity in newer APIS

昔日梦未散 2024-11-26 05:49:39

你可以这样做:

ClipboardManager myClipboard = myClipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
ClipData myClip;
EditText editText = (EditText) findViewById(R.id.editText3);
int min = 0;
int max = editText.getText().length();
if (editText.isFocused()) {
    final int selStart = editText.getSelectionStart();
    final int selEnd = editText.getSelectionEnd();
    min = Math.max(0, Math.min(selStart, selEnd));
    max = Math.max(0, Math.max(selStart, selEnd));
}
// here is your selected text
final CharSequence selectedText = editText.getText().subSequence(min, max);
String text = selectedText.toString();


// copy to clipboard
myClip = ClipData.newPlainText("text", text);
myClipboard.setPrimaryClip(myClip);

用 TextView 替换 EditText

you can do it this way:

ClipboardManager myClipboard = myClipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
ClipData myClip;
EditText editText = (EditText) findViewById(R.id.editText3);
int min = 0;
int max = editText.getText().length();
if (editText.isFocused()) {
    final int selStart = editText.getSelectionStart();
    final int selEnd = editText.getSelectionEnd();
    min = Math.max(0, Math.min(selStart, selEnd));
    max = Math.max(0, Math.max(selStart, selEnd));
}
// here is your selected text
final CharSequence selectedText = editText.getText().subSequence(min, max);
String text = selectedText.toString();


// copy to clipboard
myClip = ClipData.newPlainText("text", text);
myClipboard.setPrimaryClip(myClip);

Replace EditText with TextView

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文