如何获取从剪贴板复制的数据,并将其设置为安卓工作室中的另一个值。

huangapple go评论82阅读模式
英文:

How to get the value of data copied to clipboard and set it to another value in android studio

问题

我想知道如何能够将复制到剪贴板的特定数据的值更改为我自己设定的值。

例如,如果应用程序注意到一个电话号码已经被复制到剪贴板,它可以将其更改为我设定的电话号码。

附注:我在Android开发和Java编码方面非常新手。

英文:

I would like to know how I can be able to change the value of a specific data copied into my clipboard to my own set value.

So for instance, if the application notices that a phone number had been copied into clipboard, it could then change it to a set phone number be me.

Ps: I'm very new to android development and Java coding.

答案1

得分: 1

从剪贴板获取数据应该像这样:

ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
ClipData.Item item = clipboard.getPrimaryClip().getItemAt(0);
pasteData = item.getText();

您确实需要检查有关pasteData的一些情况以正确使用它:它是否是文本?它是否为null?...

将值设置到剪贴板应该像这样:

ClipData clip = ClipData.newPlainText("simple text", "Hello, World!");
clipboard.setPrimaryClip(clip);

您不需要更改现有的值,只需创建一个新的剪贴板项。

这仅是一个大致的解释,因为我不太了解您的用例,但我建议您查阅以下文档以获取更多详细信息:
https://developer.android.com/guide/topics/text/copy-paste#java

英文:

Getting data from the clipboard should look like this :

ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
ClipData.Item item = clipboard.getPrimaryClip().getItemAt(0);
pasteData = item.getText();

You do have to check a few things about pasteData to use it properly : is it text ? is it null ? ...

Setting a value in the clipboard should look like this :

ClipData clip = ClipData.newPlainText("simple text", "Hello, World!");
clipboard.setPrimaryClip(clip);

You don't have to change an existing value, just create a new one.

This is all just a rough explanation as I don't really know what your use case is but I recommend you check out this documentation for more details :
https://developer.android.com/guide/topics/text/copy-paste#java

huangapple
  • 本文由 发表于 2020年4月3日 20:28:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/61011927.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定