public boolean onDrag(View v, DragEvent event) { final int action = event.getAction(); switch (action) { case DragEvent.ACTION_DRAG_STARTED: if (event.getClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) { ((ImageView) v).setColorFilter(Color.BLUE); Toast.makeText(DragAndDropActivity.this, "ACTION_DRAG_STARTED", Toast.LENGTH_SHORT) .show(); v.invalidate(); return true; } else { return false; } case DragEvent.ACTION_DRAG_ENTERED: ((ImageView) v).setColorFilter(Color.GREEN); Toast.makeText(DragAndDropActivity.this, "ACTION_DRAG_ENTERED", Toast.LENGTH_SHORT) .show(); v.invalidate(); return true; case DragEvent.ACTION_DRAG_LOCATION: return true; case DragEvent.ACTION_DRAG_EXITED: ((ImageView) v).setColorFilter(Color.RED); Toast.makeText(DragAndDropActivity.this, "ACTION_DRAG_EXITED", Toast.LENGTH_SHORT).show(); v.invalidate(); return true; case DragEvent.ACTION_DROP: ClipData.Item item = event.getClipData().getItemAt(0); CharSequence dragData = item.getText(); Toast.makeText( DragAndDropActivity.this, "Dragged data is " + dragData, Toast.LENGTH_SHORT) .show(); ((ImageView) v).clearColorFilter(); v.invalidate(); return true; case DragEvent.ACTION_DRAG_ENDED: ((ImageView) v).clearColorFilter(); v.invalidate(); if (event.getResult()) { Toast.makeText(DragAndDropActivity.this, "The drop was handled.", Toast.LENGTH_SHORT) .show(); } else { Toast.makeText(DragAndDropActivity.this, "The drop didn't work.", Toast.LENGTH_SHORT) .show(); } ; return true; default: Log.e("DragDrop Example", "Unknown action type received by OnDragListener."); break; } ; return false; };
/** * 粘贴【剪切板】功能 * * @return 返回剪贴板的内容 */ @TargetApi(Build.VERSION_CODES.HONEYCOMB) public String paste() { // 粘贴部分【本案例不需要粘贴】 ClipboardManager myClipboard; myClipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE); // 需要做的下一件事就是通过调用ClipData类的数据方法的相应类型来实例化ClipData对象。如果文本数据在newPlainText方法被调用。必须将数据设置为剪贴板管理器对象的剪辑。 ClipData clipData = myClipboard.getPrimaryClip(); ClipData.Item item = clipData.getItemAt(0); return item.getText().toString(); }
@Override public boolean onDrag(View v, DragEvent event) { // Defines a variable to store the action type for the incoming event final int action = event.getAction(); switch (action) { case DragEvent.ACTION_DRAG_STARTED: // Log.d(TAG, "onDrag() ACTION_DRAG_STARTED"); return true; case DragEvent.ACTION_DRAG_ENTERED: Log.d(TAG, "onDrag() ACTION_DRAG_ENTERED"); return true; case DragEvent.ACTION_DRAG_LOCATION: // Log.d(TAG, "onDrag() ACTION_DRAG_LOCATION"); return true; case DragEvent.ACTION_DRAG_EXITED: Log.d(TAG, "onDrag() ACTION_DRAG_EXITED"); return true; case DragEvent.ACTION_DROP: Log.d(TAG, "onDrag() ACTION_DROP"); // Gets the item containing the dragged data ClipData.Item item = event.getClipData().getItemAt(0); String sku = item.getText().toString(); if (v.getId() == R.id.basketImageView) { Toast.makeText(this, "Dropped " + sku, Toast.LENGTH_LONG).show(); } return true; case DragEvent.ACTION_DRAG_ENDED: // Log.d(TAG, "onDrag() ACTION_DRAG_ENDED"); return true; // An unknown action type was received. default: Log.e(TAG, "onDrag() Unknown DragEvent"); break; } Log.d(TAG, "onDrag() Exit with false"); return false; }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // setContentView(R.layout.activity_test_hook_binder); try { BinderHookHelper.hookClipboardService(); } catch (Exception e) { e.printStackTrace(); } EditText editText = new EditText(this); setContentView(editText); ClipboardManager manager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); if (manager.hasPrimaryClip()) { ClipData data = manager.getPrimaryClip(); ClipData.Item item = data.getItemAt(0); editText.setText(item.getText().toString()); } }
/** * Return the content text. If there is intent in clip data, display intent, or display uri if uri * included, or html and text at last. * * @return the content in string. */ public String getDiaplayText() { String ret = null; if (this.isCoerceText()) { ret = this.getContent(); } else { ClipData.Item item = this.rawContent.getItemAt(0); CharSequence text = item.getText(); String html = item.getHtmlText(); Intent intent = item.getIntent(); Uri uri = item.getUri(); if (intent != null) { ret = intent.toUri(Intent.URI_INTENT_SCHEME); } else if (uri != null) { ret = uri.toString(); } else if (html != null) { ret = html; } else if (text != null) { ret = text.toString(); } } return ret; }
public void PasteFromClipboard() { ClipData cdata = mClipBoard.getPrimaryClip(); ClipData.Item item = cdata.getItemAt(0); this.setText(item.getText().toString()); }