DragAndDrop:接收拖放内容 · AndroidX 源码指南
AAndroidX 源码指南
基础工具
基础工具 · androidx.collection

DragAndDrop:接收拖放内容Collection 1.7.0-alpha01

DropHelper 配置拖放目标,接收从其他应用拖入的文本、URI 等。

最后更新 2026-08-01

复制即用

import androidx.draganddrop.DropHelper;

DropHelper.configureView(
    activity,
    targetView,
    new DropHelper.Options.Builder()
        .setHighlightColor(color)
        .build(),
    new String[] { "text/plain", "image/*" },
    (view, event) -> {
        ClipData clip = event.getClipData();
        // 读取拖入的内容
        if (clip != null && clip.getItemCount() > 0) {
            CharSequence text = clip.getItemAt(0).coerceToText(context);
            handleDroppedText(text.toString());
        }
        return true;
    }
);

要点

  • configureView 把指定 View 配置为拖放目标,自动处理高亮与事件。
  • 第三参数为接受的 MIME 类型(text/plainimage/* 等)。
  • 回调收到 DragEvent,通过 getClipData() 读取内容。
  • DropHelper.Options 支持高亮颜色、圆角半径、内部 EditText 列表。
  • 适合”从其他应用拖文字/图片进来”的场景;应用内拖放用普通 DragEvent 即可。

On this page