android文本长按复制

📖1. 长按直接复制

11

✅步骤一:定义一个TextView

<TextView
    android:id="@+id/tv_content"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="我是测试文本"
    android:textSize="16sp"/>

✅步骤二:为TextView注册长按事件

在Activity或Fragment中找到TextView并为其注册长按事件,代码如下:

TextView tvContent = findViewById(R.id.tv_content);
tvContent.setOnLongClickListener(new View.OnLongClickListener() {
    @Override
    public boolean onLongClick(View v) {
        // 在这里处理长按事件
        return true;
    }
});

✅步骤三:弹出系统复制功能

在长按事件中弹出系统复制功能,代码如下:

TextView tvContent = findViewById(R.id.tv_content);
tvContent.setOnLongClickListener(new View.OnLongClickListener() {
    @Override
    public boolean onLongClick(View v) {
        ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
        ClipData clip = ClipData.newPlainText("text", tvContent.getText());
        clipboard.setPrimaryClip(clip);
        Toast.makeText(MainActivity.this, "文本已复制", Toast.LENGTH_SHORT).show();
        return true;
    }
});

注意:如何在适配器中使用,需要使用上下文Context.getSystemService方法即可

📖2. 长按弹框确认复制

22

✅步骤一:定义一个TextView

<TextView
    android:id="@+id/tv_content"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="我是测试文本"
    android:textSize="16sp"/>

✅步骤二:封装PopupWindow弹框方法

1.定义layout_popup_window.xml布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <Button
        android:id="@+id/btn_copy"
        android:layout_width="wrap_content"
        android:layout_height="45dp"
        android:text="复制文本" />

</LinearLayout>

2.可创建一个工具类,比如SystemUtils中去封装这个方法,好处就是复用性强,也可直接定义在要使用的页面中

public class SystemUtils{
     /**
     * 弹框复制文本-文本下面位置
     * @param context 上下文
     * @param anchorView 被复制文本视图
     * @param textToCopy 被复制的文本内容
     */
    public static void showCopyPopupWindow(final Context context, final View anchorView, final String textToCopy) {
        // 创建 PopupWindow
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View popupView = inflater.inflate(R.layout.layout_popup_window, null);
        Button copyButton = popupView.findViewById(R.id.btn_copy);
        final PopupWindow popupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);

        // 设置按钮点击事件
        copyButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 获取剪贴板管理器
                ClipboardManager clipboardManager = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
                // 创建 ClipData 对象
                ClipData clipData = ClipData.newPlainText("text", textToCopy);
                // 将 ClipData 对象放入剪贴板
                clipboardManager.setPrimaryClip(clipData);
                Toast.makeText(context, "复制成功", Toast.LENGTH_SHORT).show();
                // 关闭 PopupWindow
                popupWindow.dismiss();
            }
        });

        // 计算文本的位置
        int[] location = new int[2];
        anchorView.getLocationInWindow(location);
        int x = location[0];
        int y = location[1] + anchorView.getHeight();

        // 显示 PopupWindow
        popupWindow.showAtLocation(anchorView, Gravity.NO_GRAVITY, x, y);
    }
}

✅步骤三:为TextView注册长按事件

TextView tvContent = findViewById(R.id.tv_content);
tvContent.setOnLongClickListener(new View.OnLongClickListener() {
    @Override
    public boolean onLongClick(View v) {
        // 在这里处理长按事件
        return true;
    }
});

✅步骤四:调用弹框复制确认方法

TextView tvContent = findViewById(R.id.tv_content);
tvContent.setOnLongClickListener(new View.OnLongClickListener() {
    @Override
    public boolean onLongClick(View view) {
        // 在这里处理长按事件
        final String textToCopy = ((TextView) view).getText().toString();
        SystemUtils.showCopyPopupWindow(view.getContext(), view, textToCopy);
        return true;
    }
});
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小满@

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值