import android.view.View;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.widget.TextView;
import android.widget.Toast;
/**
* Created by wanghailong on 2017/6/28.
*/
public class CopyUtil {
public static void copy(final Context ctx, final TextView tv) {
if (null == tv || null == ctx) {
return;
}
tv.setOnLongClickListener(new android.view.View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
// 复制到剪贴板管理器
ClipboardManager cmb = (ClipboardManager) ctx.getSystemService(Context.CLIPBOARD_SERVICE);
ClipData myClip;
myClip = ClipData.newPlainText("text", tv.getText().toString());
cmb.setPrimaryClip(myClip);
Toast.makeText(ctx, "已复制在剪贴板",
Toast.LENGTH_SHORT).show();
return true;
}
});
}
}
android中复制到剪贴板功能的代码
最新推荐文章于 2023-08-04 22:55:24 发布