99

According to the Android documentation, ClipData use "label" as a kind of representation to the copied data.

ClippedData is a complex type containing one or Item instances, each of which can hold one or more representations of an item of data. For display to the user, it also has a label and iconic representation.

And then it further explains "label" as User-visible label for the clip data in some API docs. However, I'm still confused about the usage of the label.

How is this label visible to users? How should I use it? What should I set for this label when I call the ClipData factory method newPlainText(CharSequence label, CharSequence text)? for example:

private void copyToClipBoard() {

    ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
    ClipData clip = ClipData.newPlainText(
            "text label", // What should I set for this "label"?
            "content to be copied");
    clipboard.setPrimaryClip(clip);
    Toast.makeText(AboutActivity.this, "Saved to clip board", Toast.LENGTH_SHORT).show();
}
4

4 回答 4

113
ClipData clip = ClipData.newPlainText(
            "text label", 
            "content to be copied");

这里的文本标签描述了剪辑中的数据

例如。

ClipData clip = ClipData.newPlainText(
            "user Name",
            user.getName()); 

我们可以使用

clip.getDescription ();
于 2015-10-19T06:31:38.797 回答
30

似乎文档中的“剪辑数据的用户可见标签”描述应该被解释为您作为开发人员可以设置然后自己向用户显示的内容,而不是 Android 系统将向用户显示的内容。

查看 Android 源代码时,ClipDescription.getLabel()方法似乎在 Android 5.0 之前未使用。在 5.0 RemoteInput中,RemoteInputCompatJellybean 和 com.android.mail.compose.ComposeActivity 都声明了使用方法

如果您查看使用情况,所有这些都设置了一个标签,该标签不打算被用户看到,而是用于以编程方式识别代码中不同位置的剪辑。

查看如何在 Android中使用ClipData.newPlainText()时,大多数情况下 null 作为标签给出,这表明该标签并没有真正用于任何事情。

当然,某些手机制造商或其他应用程序开发人员可能会在某些情况下获取标签并将其显示给用户。但一般来说,如果您自己显示剪辑的标签,则应该安全地假设它只会在您的应用程序中显示给用户。

于 2016-09-15T07:01:02.170 回答
4

今天在开发我的应用程序时,我发现了 ClipData 标签的一个用例。一些应用程序将其设置为 null,而其他应用程序几乎使用它。

对于我的应用程序,我正在听 ClipManager 的addPrimaryClipChangedListener

我在几乎一直在后台运行的服务类中执行此操作。我想处理从我的应用程序中添加到 primaryClip 的数据与在另一个应用程序中添加的数据不同(比如说在网络浏览器中复制的文本)。

这是我的代码摘录以及我如何使用 ClipData 标签:

mClipBoardManager.addPrimaryClipChangedListener(new ClipboardManager.OnPrimaryClipChangedListener() {
            @Override
            public void onPrimaryClipChanged() {
                String clipLabel = "default";
                if (mClipBoardManager.getPrimaryClip().getDescription().getLabel() != null) {
                    clipLabel = mClipBoardManager.getPrimaryClip().getDescription().getLabel().toString();
                }
                if (clipLabel.equals("auto_copy_text")) {
                    //TODO: Text from my app do stuffs you will do with text from my app
                } else {
                    //TODO: Text from some other app
                }

            }
        });

在我的应用程序中,当我向 primaryClip 添加数据时,我包含如下标签:

private void addToClipboard(String text) {
        mClipboardManager = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
        mClipboardManager.setPrimaryClip(ClipData.newPlainText("auto_copy_text", text));
    }

我希望这有帮助

于 2019-03-25T12:00:10.387 回答
0

我注意到的另一件事是,如果用户再次复制具有相同标签的数据,则先前具有相同标签的文本将被覆盖。所以一个标签只能保留一份数据副本,有助于清除以前的混乱。标签还可用于识别您的唯一文本,即使它不是用户复制的最后一件事,也可用于检索您的文本数据。

于 2021-05-04T11:34:34.627 回答