Android复制并粘贴多个文本 [英] Android Copy and Paste Multiple Text

查看:586
本文介绍了Android复制并粘贴多个文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已使用android剪贴板管理器复制和粘贴文本.像whatsapp一样,我想从listview复制多个文本并将其粘贴. 我能够做到这一点,

I have used android clipboard manager to copy and paste text. Like whatsapp, i would like to copy multiple text from listview and paste those. I am able to do like this,

StringBuilder textMessage = new StringBuilder();
for(messsage) {
 textmessage.append(message);
 textmessage.append("\n"); 
}
ClipData clip = ClipData.newPlainText("simple text", textMessage.toString());
clipboard.setPrimaryClip(clip);

代替将多个文本消息附加到一个文本消息中,我可以将文本消息的数组存储在一个剪辑对象中并使用数组索引进行检索.

Instead of appending the multiple textmessages into one, can i able to store the array of textmessages into one clip object and retrive using array indices.

推荐答案

我想,您可以在ClipData中添加多个ClipData.Item.因此,不要使用静态方法newPlainText,而是使用

I guess, you could add multiple ClipData.Item to your ClipData. So instead of using static method newPlainText, create your new ClipData using

ClipData(ClipDescription description, ClipData.Item item)

或任何其他可用的构造函数.

or any other constructor available.

我已经使用ClipDatagetItemCount方法来证明它确实是值的索引列表,因此,只要位置不引导您,就可以肯定地使用getItemAt从任何位置获取任何ClipData.Item.到OutOfBoundException.下面的代码是新手,但是我相信可以达到演示的目的.让我知道您是否需要更多帮助.

I have used getItemCount method of ClipData to demonstrate that it is indeed a indexed list of values, so you can definitely use getItemAt to fetch any ClipData.Item from any position, provided position is not leading you to OutOfBoundException. Below code is very novice, but would serve the purpose of demonstration I believe. Let me know if you need any more help.

public class MainActivity extends AppCompatActivity {
    ClipboardManager clipboard;
    static int var = 0;
    ClipData clipData;
    TextView tvClip;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tvClip = (TextView) findViewById(R.id.tv_add);

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

        Button btnClip = (Button) findViewById(R.id.btn_add);
        btnClip.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ClipData.Item item = new ClipData.Item("var" + var);
                if (clipData == null) {
                    clipData = new ClipData(new ClipDescription("your_clip_description", new String[]{ClipDescription.MIMETYPE_TEXT_PLAIN}), item);
                    clipboard.setPrimaryClip(clipData);
                }
                clipData.addItem(item);
            }
        });

        Button showClip = (Button) findViewById(R.id.btn_show);
        showClip.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (clipData != null)
                    tvClip.setText("count = " + clipData.getItemCount());
            }
        });
    }
}

这篇关于Android复制并粘贴多个文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆