CopyTextView

public class CopyTextView extends TextView{

    public CopyTextView(Context context) {
        super(context);
        initLongClickAction();
    }

    public CopyTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initLongClickAction();
    }

    public CopyTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initLongClickAction();
    }

    void initLongClickAction(){
        setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(final View v) {
                final TextView mv = ((TextView) v);
                SystemService.getVibrator(v.getContext()).vibrate(ConstantValue.VibratorDuration);
                final PopupMenu popup = new PopupMenu(mv.getContext(), mv);
                popup.getMenuInflater()
                        .inflate(R.menu.poupup_copy, popup.getMenu());
                popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    public boolean onMenuItemClick(MenuItem item) {
                        popup.dismiss();
                        switch (item.getItemId()) {
                            case R.id.copy:
                                ClipUtil.copy(mv.getContext(), mv.getText().toString());
                                break;
                            case R.id.cancel:
                                break;
                        }
                        return true;
                    }
                });
                popup.show();
                return true;
            }
        });
    }

}

poupup_copy.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/copy"
        android:title="@string/copy"/>

    <item
        android:id="@+id/cancel"
        android:title="@string/cancel"/>
</menu>
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页