在 Android 中剪切、复制、粘贴

发布于 2024-11-10 18:42:13 字数 286 浏览 1 评论 0原文

我想在我的 EditText 中实现剪切、复制、粘贴功能,我尝试使用以下代码:

    ClipMan = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
    editbox1.setText(ClipMan.getText());

用于粘贴文本,但它将整个文本粘贴到另一个编辑框中。我想复制所选文本并将该文本粘贴到相同的编辑框就像普通记事本一样工作..

非常感谢任何建议... 谢谢 !!

I want to implement the cut,copy, paste functionality in my EditText,i tried with the following code :

    ClipMan = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
    editbox1.setText(ClipMan.getText());

for paste the text,but it paste the whole text in another Editbox..I want to copy the selected text and paste that text in the same Editbox just like the normal notepad works..

Any suggestions are greatly appreciated...
Thanks !!

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

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

发布评论

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

评论(3

静若繁花 2024-11-17 18:42:13

要复制数据

ClipData clip = ClipData.newPlainText("simple text",Value);
ClipMan.setPrimaryClip(clip);

并获取在您的情况下复制的数据,

CharSequence pasteData="";
ClipData.Item item = clipboard.getPrimaryClip().getItemAt(0);
pasteData = item.getText();

您可以使用 EditText.getText() 而不是 Value
EditText.setText(pasteData); 用于粘贴该数据。

for copy data

ClipData clip = ClipData.newPlainText("simple text",Value);
ClipMan.setPrimaryClip(clip);

and get data which was copied

CharSequence pasteData="";
ClipData.Item item = clipboard.getPrimaryClip().getItemAt(0);
pasteData = item.getText();

in your case you can use EditText.getText() instead of Value..
and EditText.setText(pasteData); for paste that data.

抚笙 2024-11-17 18:42:13

最后,我可以在我的应用程序中复制、粘贴。现在我可以使用以下代码仅粘贴选定的文本:

Editable s1;
EditText editbox2;

复制选定的文本:

       if(editbox2.getSelectionEnd() > editbox2.getSelectionStart()) 
        {
            s1 = (Editable) editbox2.getText().subSequence(editbox2.getSelectionStart(), editbox2.getSelectionEnd());
        }else
        {
            s1 = (Editable) editbox2.getText().subSequence(editbox2.getSelectionEnd(), editbox2.getSelectionStart());
        }

粘贴选定的文本:

             editbox2.getText().replace(Math.min(editbox2.getSelectionStart(),editbox2.getSelectionEnd()), Math.max(editbox2.getSelectionStart(), editbox2.getSelectionEnd()),s1, 0, s1.length());

Finally i am able to copy ,paste in my application..now i can paste only selected text by using this code :

Editable s1;
EditText editbox2;

to copy the selected text :

       if(editbox2.getSelectionEnd() > editbox2.getSelectionStart()) 
        {
            s1 = (Editable) editbox2.getText().subSequence(editbox2.getSelectionStart(), editbox2.getSelectionEnd());
        }else
        {
            s1 = (Editable) editbox2.getText().subSequence(editbox2.getSelectionEnd(), editbox2.getSelectionStart());
        }

to paste the selected text:

             editbox2.getText().replace(Math.min(editbox2.getSelectionStart(),editbox2.getSelectionEnd()), Math.max(editbox2.getSelectionStart(), editbox2.getSelectionEnd()),s1, 0, s1.length());
拒绝两难 2024-11-17 18:42:13

我们可以这样做:

    int min2 = 0;
    int max2 = editText.getText().length();
    if (editText.isFocused()) {
        final int selStart = editText.getSelectionStart();
        final int selEnd = editText.getSelectionEnd();

        min2 = Math.max(0, Math.min(selStart, selEnd));
        max2 = Math.max(0, Math.max(selStart, selEnd));
     }
     ClipData abc = myClipboard.getPrimaryClip();
     ClipData.Item item = abc.getItemAt(0);

     String text = item.getText().toString();
     editText.getText().replace(min2,max2 , text);

we can do this way :

    int min2 = 0;
    int max2 = editText.getText().length();
    if (editText.isFocused()) {
        final int selStart = editText.getSelectionStart();
        final int selEnd = editText.getSelectionEnd();

        min2 = Math.max(0, Math.min(selStart, selEnd));
        max2 = Math.max(0, Math.max(selStart, selEnd));
     }
     ClipData abc = myClipboard.getPrimaryClip();
     ClipData.Item item = abc.getItemAt(0);

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