代码调用系统复制粘贴板
//获取剪贴板管理器: 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