val clipboard=requireActivity().getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager clipboard.text = obj.toString().trim({ it <= ' ' }) val clipData = ClipData.newPlainText(null, obj.toString()) clipboard.setPrimaryClip(clipData)
获取系统剪贴板内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
ClipboardManager manager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); if (manager != null) { if (manager.hasPrimaryClip() && manager.getPrimaryClip().getItemCount() > 0) { CharSequence charSequence_addedText = manager.getPrimaryClip().getItemAt(0).getText(); String addedTextString = String.valueOf(charSequence_addedText); try { JSONObject obj = new JSONObject(addedTextString); JSONArray scanner = obj.getJSONArray("scanner"); StringBuffer stringBuffer_scanner = new StringBuffer(); for (int i = 0; i < scanner.length(); i++) { String s = (String) scanner.get(i); stringBuffer_scanner.append(s); } tvFragXsScanner.setText(stringBuffer_scanner.toString()); } catch (JSONException e) { e.printStackTrace(); } } }