如何在ACTION_DRAG_STARTED上保存拖动按钮的文字?

时间:2014-05-06 17:42:51

标签: java android button drag-and-drop

我想保存拖动按钮的文字,将其与其他按钮的文字交换 - 我将其放入其中。

到目前为止,我有这个:

b.setOnDragListener( new OnDragListener(){

    Drawable defaultBackground = ((Button) b).getBackground();

    @Override
    public boolean onDrag(View v,  DragEvent event){
      String draggedWord = "";

      switch(event.getAction())                   
      {
         case DragEvent.ACTION_DRAG_STARTED:
            Log.d(USER_SERVICE, "Action is DragEvent.ACTION_DRAG_STARTED");
            // ... what here ?
            // if I put draggedWord = ((Button) v).getText().toString();
            // it's triggered for every button with this listener
            Log.d(USER_SERVICE, "DraggedWord" + draggedWord);
            break;
         case DragEvent.ACTION_DRAG_ENTERED:
            Log.d(USER_SERVICE, "Action is DragEvent.ACTION_DRAG_ENTERED");
            ((Button) v).setBackgroundColor(Color.GREEN);
            break;
         case DragEvent.ACTION_DRAG_EXITED :
            Log.d(USER_SERVICE, "Action is DragEvent.ACTION_DRAG_EXITED");
            ((Button) v).setBackground(defaultBackground);
            break;
         case DragEvent.ACTION_DRAG_LOCATION  :
            break;
         case DragEvent.ACTION_DRAG_ENDED   :
            Log.d(USER_SERVICE, "Action is DragEvent.ACTION_DRAG_ENDED");
            ((Button) v).setBackground(defaultBackground);
            break;
         case DragEvent.ACTION_DROP:
            Log.d(USER_SERVICE, "ACTION_DROP event");
            if( event.getResult() ){
              ((Button) v).setText(draggedWord);
            }
            break;
         default: 
           break;
         }
         return true;
    }
 });

event.getClipData()虽然我在onLongClickListener

中添加了一些数据,但仍返回null
  b.setOnLongClickListener(new View.OnLongClickListener() {
    @Override
    public boolean onLongClick(View v) {
       ClipData.Item item = new ClipData.Item( ((Button) v).getText() );

       String[] mimeTypes = {ClipDescription.MIMETYPE_TEXT_PLAIN};
       ClipData dragData = new ClipData(
           ((Button) v).getText().toString(), 
           mimeTypes, 
           item);

       // Instantiates the drag shadow builder.
       View.DragShadowBuilder myShadow = new DragShadowBuilder(b);

       // Starts the drag
       v.startDrag(dragData,  // the data to be dragged
                   myShadow,  // the drag shadow builder
                   null,      // no need to use local data
                   0          // flags (not currently used, set to 0)
       );
       return true;
    }
 });

修改

这是我取消注释ACTION_DRAG_STARTED部分中的块时所得到的(这是我将按钮拖到另一个按钮上的结果):

  3177                   user  D  Action is DragEvent.ACTION_DRAG_STARTED
  3177                   user  D  DraggedWord dach
  3177                   user  D  Action is DragEvent.ACTION_DRAG_STARTED
  3177                   user  D  DraggedWord kot
  3177                   user  D  Action is DragEvent.ACTION_DRAG_STARTED
  3177                   user  D  DraggedWord kubek
  3177                   user  D  Action is DragEvent.ACTION_DRAG_STARTED
  3177                   user  D  DraggedWord samochód
  3177                   user  D  Action is DragEvent.ACTION_DRAG_ENTERED
  3177                   user  D  Action is DragEvent.ACTION_DRAG_EXITED
  3177                   user  D  Action is DragEvent.ACTION_DRAG_ENTERED
  3177                   user  D  ACTION_DROP event
  3177                   user  D  DragEvent: false
  3177           ViewRootImpl  I  Reporting drop result: true
  3177                   user  D  Action is DragEvent.ACTION_DRAG_ENDED
  3177                   user  D  DragEvent: true
  1259          WindowManager  W  Drag is in progress but there is no drag window handle.
  3177                   user  D  Action is DragEvent.ACTION_DRAG_ENDED
  3177                   user  D  DragEvent: true
  3177                   user  D  Action is DragEvent.ACTION_DRAG_ENDED
  3177                   user  D  DragEvent: true
  3177                   user  D  Action is DragEvent.ACTION_DRAG_ENDED
  3177                   user  D  DragEvent: true

EDIT2

好的,所以我到了用以下内容提取所需字符串的地方:

 event.getClipDescription().toString();

但这给了我以下字符串:

DraggedWord ClipDescription { text/plain "drukarka" }

getClipData返回null。

1 个答案:

答案 0 :(得分:0)

好的,我已经设法通过

做到了
String currWord = event.getClipDescription().toString().split("\"")[1];
if( ! currWord.equals("")) 
  draggedWord = currWord;