Copy to clipboard the content of a cardview

2019-01-29 13:36发布

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(), content cannot be resolved.

4条回答
欢心
2楼-- · 2019-01-29 13:53

Try the following

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();

            }
        });
查看更多
Viruses.
3楼-- · 2019-01-29 13:54
 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.cardItems.get(position).content);
                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"));
            }
        });
    }
查看更多
别忘想泡老子
4楼-- · 2019-01-29 13:58

Copy to clipboard the content of a cardview (recyclerview ) Try this code.

@Override
protected void populateViewHolder(Attitude.BlogViewHolder viewHolder, Blog model, int position) {
    viewHolder.setText(model.getText());

    final String text = model.getText();
    viewHolder.copy.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

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

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

        }
    });
}

public static class BlogViewHolder extends RecyclerView.ViewHolder {

    View mView;
    Button copy;

    public BlogViewHolder(View itemView) {
        super(itemView);
        mView = itemView;
        this.copy= (Button)itemView.findViewById(R.id.copy_btn);
    }
}
查看更多
闹够了就滚
5楼-- · 2019-01-29 14:04

The variable v is a Button, it not have a text, you have to do it in your EditText or TextView (where you have your text to be copied)

查看更多
登录 后发表回答