Android TextView长按复制实现,Android复制文本,Selectable

 代码调用系统复制粘贴板

              //获取剪贴板管理器:
                ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
// 创建普通字符型ClipData
                ClipData mClipData = ClipData.newPlainText("Label", "这里是要复制的文字");
// 将ClipData内容放到系统剪贴板里。
                cm.setPrimaryClip(mClipData);

自定义textview

public class SelectableTextView extends AppCompatTextView {

    private SelectableTextHelper mSelectableTextHelper;
    private int selectedColor;
    private int cursorHandleColor;

    public SelectableTextView(Context context) {
        super(context);
    }

    public SelectableTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context,attrs);
    }

    public SelectableTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context,attrs);
    }

    private void init(Context context,AttributeSet attrs){
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.SelectableTextView);

        selectedColor = ta.getColor(R.styleable.SelectableTextView_selected_color, ContextCompat.getColor(context,R.color.colorAccent));
        cursorHandleColor = ta.getColor(R.styleable.SelectableTextView_cursor_handle_color, ContextCompat.getColor(context,R.color.colorAccent));

        mSelectableTextHelper = new SelectableTextHelper.Builder(this)
                .setSelectedColor(selectedColor)
                .setCursorHandleSizeInDp(20)
                .setCursorHandleColor(cursorHandleColor)
                .build();
    }


    public void setSelectedColor(int selectedColor) {
        this.selectedColor = selectedColor;
    }

    public void setCursorHandleColor(int cursorHandleColor) {
        this.cursorHandleColor = cursorHandleColor;
    }

}

layout文件应用

<com.wulee.selectabletext.SelectableTextView
    android:id="@+id/tv_test"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    android:lineSpacingExtra="3dp"
    android:text="@string/long_text"
    android:textSize="16sp"
    app:selected_color="@color/holo_orange_dark"
    app:cursor_handle_color="@color/holo_blue_bright"/>

demo连接:安卓长按复制文本demo-Android文档类资源-CSDN下载 

bug交流:qq1085220040 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值