选择用于复制Android EditText的文本

时间:2011-12-22 18:00:59

标签: android android-layout android-widget

我想在EditText中选择一些文本,它是ListView的子元素。必须在剪贴板上设置所选文本。我无法找到任何关于此的例子。我该怎么办呢? selectionStart和selectionEnd不适用于此。 感谢。

5 个答案:

答案 0 :(得分:2)

这是可能的解决方案。在listview的getView方法中执行以下操作:

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

答案 1 :(得分:1)

在列表选择事件

上使用此代码

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

答案 2 :(得分:0)

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

答案 3 :(得分:0)

您可以在editText长按事件inlistView自定义适配器上打开一个对话框,并显示两个选项复制并粘贴到其中

您可以使用

以编程方式复制文本
ClipboardManager clipboard = 
                              (ClipboardManager) c1.getSystemService(c1.CLIPBOARD_SERVICE); 

                         clipboard.setText("Text to copy");

并使用

获取文本
System.out.println(clipboard.getText());

答案 4 :(得分:0)

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) {
}

使用ExtractedText提供的api