复制到剪贴板中的cardview内容 [英] Copy to clipboard the content of a cardview

查看:169
本文介绍了复制到剪贴板中的cardview内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了回收卡视图,并希望使用按钮单击方法来复制cardView的内容。在cardview中有两个文本,我只想复制内容,不同的卡片不同。我怎样才能做到这一点?
这是我的Cardview适配器。

I've implemented a recycler card view and want to use a button click method to copy the content of cardView. There aretwo texts in cardview and i want to copy the content only, different for different cards. How can I do this? Here is my Cardview adapter.

  public static class ViewHolder extends RecyclerView.ViewHolder{
        Button copyButton;
        Button shareButton;


        TextView title;
        TextView content;
        public ViewHolder(View itemView) {
            super(itemView);
            this.title = (TextView)itemView.findViewById(R.id.card_title);
            this.content = (TextView)itemView.findViewById(R.id.card_content);
            this.copyButton= (Button)itemView.findViewById(R.id.copyButton);
            this.shareButton=(Button)itemView.findViewById(R.id.shareButton);


        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item,parent,false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.title.setText(cardItems.get(position).title);
        holder.content.setText(cardItems.get(position).content);
        holder.copyButton.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){


                myClipboard = (ClipboardManager) v.getContext().getSystemService(Context.CLIPBOARD_SERVICE);


                myClip = ClipData.newPlainText("label", v.content.getText().toString());
                myClipboard.setPrimaryClip(myClip);
                Toast.makeText(v.getContext(), "Copied to clipboard" , Toast.LENGTH_SHORT ).show();

            }
        });
        holder.shareButton.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                Intent share = new Intent(Intent.ACTION_SEND);
                share.setType("text/plain");
                share.putExtra(Intent.EXTRA_TEXT, "This message is being sent to another app");
                v.getContext().startActivity(Intent.createChooser(share, "Share Text"));
            }
        });
    }

v.content.getText()。toString(),内容无法解析。

v.content.getText().toString(), content cannot be resolved.

推荐答案

尝试以下

holder.copyButton.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){


                myClipboard = (ClipboardManager) v.getContext().getSystemService(Context.CLIPBOARD_SERVICE);


                myClip = ClipData.newPlainText("label", holder.content.getText().toString());
                myClipboard.setPrimaryClip(myClip);
                Toast.makeText(v.getContext(), "Copied to clipboard" , Toast.LENGTH_SHORT ).show();

            }
        });

这篇关于复制到剪贴板中的cardview内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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