I'm building an Android app and I want to copy the text value of an EditText widget. It's possible for the user to press Menu+A
then Menu+C
to copy the value, but how would I do this programmatically?
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
So everyone agree on how this should be done, but since no one want to give a complete solution, here goes:
I assume you have something like following declared in manifest:
Use
ClipboardManager#setPrimaryClip
method:ClipboardManager
API referenceyou can try this..
@FlySwat already gave the correct answer, I am just sharing the complete answer:
Use ClipboardManager.setPrimaryClip (http://developer.android.com/reference/android/content/ClipboardManager.html) method:
Where
label
is a User-visible label for the clip data andtext
is the actual text in the clip. According to official docs.It is important to use this import:
Here is some code to implement some copy and paste functions from EditText (thanks to Warpzit for version check). You can hook these to your button's onclick event.
Android support library update
As of Android Oreo, the support library only goes down to API 14. Most newer apps probably also have a min API of 14, and thus don't need to worry about the issues with API 11 mentioned in some of the other answers. A lot of the code can be cleaned up. (But see my edit history if you are still supporting lower versions.)
Copy
Paste
I'm adding this code as a bonus, because copy/paste is usually done in pairs.
Notes
android.content.ClipboardManager
version rather than the oldandroid.text.ClipboardManager
. Same forClipData
.context.getSystemService()
.null
. You can check each one if you find that way more readable.