android 剪下板工具類
阿新 • • 發佈:2018-12-29
public class ClipeBoardUtil { /** * 獲取剪下板裡內容 * @param context * @return */ public static String getClipeBoardContent(Context context){ ClipboardManager clipboardManager= (ClipboardManager) context.getSystemService(context.CLIPBOARD_SERVICE); ClipData primaryClip = clipboardManager.getPrimaryClip(); String content=null; if (primaryClip!=null&&primaryClip.getItemCount()>0){ ClipData.Item itemAt = primaryClip.getItemAt(0); if (itemAt!=null){ content=itemAt.getText().toString(); } } return content; } /** * 放置內容發到剪下板 */ public static void setClipeBoardContent(Context context,String content){ ClipboardManager clipboardManager= (ClipboardManager) context.getSystemService(context.CLIPBOARD_SERVICE); ClipData primaryClip = ClipData.newPlainText("Label",content);//純文字內容 clipboardManager.setPrimaryClip(primaryClip); } }