1

I'm letting the user long click on a TextView and that will copy the text of the TextView to their Clipboard.

I want it so that before it will actually copy the text of the TextView to their Clipboard, it will check if the last Clip on their Clipboard is the different than the text.

Here is the code:

ClipboardManager clipboard =(ClipboardManager)getSystemService(CLIPBOARD_SERVICE); 
String text = textView.getText().toString();

   if ( !(clipboard.getPrimaryClip().toString().equals(text)) )
// if ( !(clipboard.getPrimaryClip().equals(text)) )
   {
       clipboard.setPrimaryClip(ClipData.newPlainText("newClipName", text));
       Toast.makeText(getApplicationContext(),"Copied to clipboard.", 0).show(); 
   }

I can only imagine that the method getPrimaryClip() is not returning a String and toString() also doesn't work. How can I get the most recent Clip as a String?

4

1 回答 1

1
ClipboardManager clipboard=(ClipboardManager)getSystemService(CLIPBOARD_SERVICE);
String text = textView.getText().toString();

// item is the most recent Clip from the Clipboard
ClipData.Item item = clipboard.getPrimaryClip().getItemAt(0);

// Gets the clipboard as text.
String clipText = item.getText().toString(); // getText() returns CharSequence

if ( !(clipText.equals(text)) )
{
    clipboard.setPrimaryClip(ClipData.newPlainText("newClipName", text));
    Toast.makeText(getApplicationContext(),"Copied to clipboard.", 0).show(); 
}
于 2013-11-18T20:54:46.793 回答