Paste from clipboard in Android

I wrote a code that copies the answer in the calculator to the clipboard, then the calculator closes and another window opens. The answer should be inserted here using the code:

textOut2= (TextView) findViewById(R.id.etInput1); final ClipboardManager clipBoard= (ClipboardManager)getSystemService(CLIPBOARD_SERVICE); textOut2.setText(clipBoard.getText()); 

but it never works. Maybe a mistake? postscript I know what text is copied because I can paste using long print, but I want to do this automatically. Can I specify a specific name for the copied text? As this would make it easier to embed words, as I have many different TextView text objects

+4
source share
2 answers

public CharSequence getText () C: API Level 11 This method is deprecated. Use getPrimaryClip () instead. This extracts the main clip and tries to force it to the line.

 String textToPaste = null; ClipboardManager clipboard = (ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE); if (clipboard.hasPrimaryClip()) { ClipData clip = clipboard.getPrimaryClip(); // if you need text data only, use: if (clip.getDescription().hasMimeType(MIMETYPE_TEXT_PLAIN)) // WARNING: The item could cantain URI that points to the text data. // In this case the getText() returns null and this code fails! textToPaste = clip.getItemAt(0).getText().toString(); // or you may coerce the data to the text representation: textToPaste = clip.getItemAt(0).coerceToText(this).toString(); } if (!TextUtils.isEmpty(textToPaste)) ((TextView)findViewById(R.id.etInput1)).setText(textToPaste); 

You are allowed to add additional ClipData.Item elements with text through ClipData.addItem() , but they cannot be recognized.

+10
source

try it

 textOut2= (TextView) findViewById(R.id.etInput1); final ClipboardManager clipBoard= (ClipboardManager)getSystemService(CLIPBOARD_SERVICE); String temp = new String; temp = clipBoard.getText().toString(); textOut2.setText(temp); 
+2
source

Source: https://habr.com/ru/post/1387749/


All Articles