how to copy a text to the Clipboard in android [duplicate]

空扰寡人 提交于 2019-12-10 11:38:38

问题


I want to copy some text in temprory memory :

TextView tv = (int)findViewById(R.id.txt);
String str = tv.getText().toString();

now how an I copy str in clipboard ???


回答1:


Use ClipboardManager

int sdk = android.os.Build.VERSION.SDK_INT;
if(sdk < android.os.Build.VERSION_CODES.HONEYCOMB) {
    android.text.ClipboardManager clipboard = (android.text.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
    clipboard.setText("text to clip");
} else {
    android.content.ClipboardManager clipboard = (android.content.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); 
    android.content.ClipData clip = android.content.ClipData.newPlainText("text label","text to clip");
    clipboard.setPrimaryClip(clip);
}



回答2:


you have to use ClipboardManager.

here the doc.

you have to call setPrimaryClip with a ClipData




回答3:


You may need ClipboardManager and perhaps this could help.



来源:https://stackoverflow.com/questions/16435151/how-to-copy-a-text-to-the-clipboard-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!