将卡片视图的内容复制到剪贴板



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

  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((,内容无法解析。

尝试以下操作

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

变量v是一个Button,它没有文本,你必须在你的EditTextTextView中这样做(在那里你有你的文本要复制(

将卡片视图的内容复制到剪贴板(回收视图(试试这段代码。

@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);
    }
}
 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"));
            }
        });
    }

相关内容

  • 没有找到相关文章

最新更新