简体   繁体   中英

Select text for copying Android EditText

I want to select some text inside an EditText which is the child element of a ListView. The selected text has to be set on a clipboard. I am unable to find any examples regarding this. How should i go about this? selectionStart and selectionEnd don't work on this. Thanks.

here is the possible solution . in getView method of listview perform following operation:

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

在列表选择事件上使用此代码

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

EditText已在长按时提供此功能....意味着按下editText上的长按,弹出上下文菜单,询问全选,选择文本,全部复制。

You can open one dialog on editText long click event inlistView custom adapter and display two option copy and paste in them

you can copy text programically using

ClipboardManager clipboard = 
                              (ClipboardManager) c1.getSystemService(c1.CLIPBOARD_SERVICE); 

                         clipboard.setText("Text to copy");

and get Text Using

System.out.println(clipboard.getText());
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 given by ExtractedText

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM