英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论