Select text to copy Android EditText

I want to select text inside an EditText, which is a child of a ListView. The selected text must be set to the clipboard. I can not find any examples regarding this. How am I supposed to do this? selectionStart and selectionEnd do not work on this. Thanks.

+6
source share
5 answers

here is a possible solution. in the getView method listView, do the following:

enter code here: public View getView(final int position, View convertView, ViewGroup parent) { // A ViewHolder keeps references to children views to avoid unneccessary calls // to findViewById() on each row. final ViewHolder holder; if (convertView == null) { convertView = mInflater.inflate(R.layout.main, null); // Creates a ViewHolder and store references to the two children views // we want to bind data to. holder = new ViewHolder(); holder.subText = (TextView) convertView.findViewById(R.id.subTxt); convertView.setTag(holder); } else { // Get the ViewHolder back to get fast access to the TextView // and the ImageView. holder = (ViewHolder) convertView.getTag(); } //TEXT BOX position is 0 then if(position == 0) { ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); clipboard.setText(txtEdit.getText().toString()); } return convertView; } 
+2
source

Use this code for list select event

ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); clipboard.setText(txtEdit.getText().toString());

+1
source

EditText already provides these functions with a long press .... means pressing a long touch on editText, which appears in the context menu, asking for a selection of all, select the text, copy everything.

0
source

You can open one dialog in the editText file with a long click in the inlistView user adapter and display two options for copying and pasting into them

you can copy text with

 ClipboardManager clipboard = (ClipboardManager) c1.getSystemService(c1.CLIPBOARD_SERVICE); clipboard.setText("Text to copy"); 

and get the text with

 System.out.println(clipboard.getText()); 
0
source
 InputConnection ic = getCurrentInputConnection(); ExtractedText extracted = ic.getExtractedText( new ExtractedTextRequest(), 0); /*If selection start and end are not equal then selected text * needs to be deleted and updated to core*/ if (extracted!= null && extracted.selectionStart != extracted.selectionEnd) { } 

Use api specified by ExtractedText

0
source

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


All Articles