将卡片视图的内容复制到剪贴板
Copy to clipboard the content of a cardview
我实现了一个recycler card view,想用点击按钮的方式复制cardView的内容。 cardview中有两个文本,我只想复制内容,不同的卡片不同。我怎样才能做到这一点?
这是我的 Cardview 适配器。
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 是一个 Button
,它没有文本,你必须在你的 EditText
或 TextView
中完成它(你有你的文本要被复制)
尝试以下方法
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();
}
});
将卡片视图(recyclerview)的内容复制到剪贴板
试试这个代码。
@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"));
}
});
}
我实现了一个recycler card view,想用点击按钮的方式复制cardView的内容。 cardview中有两个文本,我只想复制内容,不同的卡片不同。我怎样才能做到这一点? 这是我的 Cardview 适配器。
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 是一个 Button
,它没有文本,你必须在你的 EditText
或 TextView
中完成它(你有你的文本要被复制)
尝试以下方法
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();
}
});
将卡片视图(recyclerview)的内容复制到剪贴板 试试这个代码。
@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"));
}
});
}