将文本复制到剪贴板

时间:2019-04-17 09:28:25

标签: android

public void onBindViewHolder(@NonNull final holderView holderView, final int position) {
     holderView.insptext.setText(inspirationItemsList.get(position).getName());
     holderView.copy.setImageResource(inspirationItemsList.get(position).getCopy());
     holderView.share.setImageResource(inspirationItemsList.get(position).getShare());



        holderView.copy.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){



                Toast.makeText(v.getContext(), "Copied to clipboard" , Toast.LENGTH_SHORT ).show();

            }
        });

2 个答案:

答案 0 :(得分:0)

如果要以编程方式复制到剪贴板,则here是参考链接:

这就是实现它的方式:

import android.content.ClipboardManager;
ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); 
ClipData clip = ClipData.newPlainText("label", "Text to copy");
clipboard.setPrimaryClip(clip);

here是ClipboardManager的文档

答案 1 :(得分:-1)

要在剪贴板中复制内容,请使用以下代码:

ClipboardManager clipboard = (ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE); 
ClipData clip = ClipData.newPlainText(label, text);
clipboard.setPrimaryClip(clip);