2014-03-01 43 views
0

大家好我正在做一個拖放應用程序。我完成了拖放部分,現在我想要做的是檢查是否所有對象/項目都已放到放置目標上,並顯示Toast消息,通知所有對象都已放棄。Android檢查是否所有對象都被丟棄

我該怎麼做?這是我迄今爲止所嘗試的:

protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.main); 

//get both sets of text views 

//views to drag 
option1 = (TextView)findViewById(R.id.option_1); 
option2 = (TextView)findViewById(R.id.option_2); 
option3 = (TextView)findViewById(R.id.option_3); 

//views to drop onto 
choice1 = (TextView)findViewById(R.id.choice_1); 
choice2 = (TextView)findViewById(R.id.choice_2); 
choice3 = (TextView)findViewById(R.id.choice_3); 

//set touch listeners 
option1.setOnTouchListener(new ChoiceTouchListener()); 
option2.setOnTouchListener(new ChoiceTouchListener()); 
option3.setOnTouchListener(new ChoiceTouchListener()); 

//set drag listeners 
choice1.setOnDragListener(new ChoiceDragListener()); 
choice2.setOnDragListener(new ChoiceDragListener()); 
choice3.setOnDragListener(new ChoiceDragListener()); 
} 


private final class ChoiceTouchListener implements OnTouchListener { 
@Override 
public boolean onTouch(View view, MotionEvent motionEvent) { 
    if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) { 
     /* 
     * Drag details: we only need default behavior 
     * - clip data could be set to pass data as part of drag 
     * - shadow can be tailored 
     */ 
     ClipData data = ClipData.newPlainText("", ""); 
     DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view); 
     //start dragging the item touched 
     view.startDrag(data, shadowBuilder, view, 0); 
     return true; 
    } else { 
     return false; 
    } 
} 
} 

@SuppressLint("NewApi") 
private class ChoiceDragListener implements OnDragListener { 

@Override 
public boolean onDrag(View v, DragEvent event) { 
    switch (event.getAction()) { 
    case DragEvent.ACTION_DRAG_STARTED: 
     //no action necessary 
     break; 
    case DragEvent.ACTION_DRAG_ENTERED: 
     //no action necessary 
     break; 
    case DragEvent.ACTION_DRAG_EXITED:   
     //no action necessary 
     break; 
    case DragEvent.ACTION_DROP: 

     //handle the dragged view being dropped over a drop view 
     View view = (View) event.getLocalState(); 
     //view dragged item is being dropped on 
     TextView dropTarget = (TextView) v; 
     //view being dragged and dropped 
     TextView dropped = (TextView) view; 
     //checking whether first character of dropTarget equals first character of dropped 
     if(dropTarget.getText().toString().charAt(0) == dropped.getText().toString().charAt(0)) 
     { 
      //stop displaying the view where it was before it was dragged 
      view.setVisibility(View.INVISIBLE); 
      //update the text in the target view to reflect the data being dropped 
      dropTarget.setText(dropTarget.getText().toString() + dropped.getText().toString()); 
      //make it bold to highlight the fact that an item has been dropped 
      dropTarget.setTypeface(Typeface.DEFAULT_BOLD); 
      //if an item has already been dropped here, there will be a tag 
      Object tag = dropTarget.getTag(); 
      //if there is already an item here, set it back visible in its original place 
      if(tag!=null) 
      { 
       //the tag is the view id already dropped here 
       int existingID = (Integer)tag; 
       //set the original view visible again 
       findViewById(existingID).setVisibility(View.VISIBLE); 
      } 
      //set the tag in the target view being dropped on - to the ID of the view being dropped 
      dropTarget.setTag(dropped.getId()); 
      //remove setOnDragListener by setting OnDragListener to null, so that no further drag & dropping on this TextView can be done 
      dropTarget.setOnDragListener(null); 
     } 
     else 
      //displays message if first character of dropTarget is not equal to first character of dropped 
      Toast.makeText(Picture_to_word_24_color.this, dropTarget.getText().toString() + "is not " + dropped.getText().toString(), Toast.LENGTH_LONG).show(); 
     break; 
    case DragEvent.ACTION_DRAG_ENDED: 
     //no action necessary 
     break; 
    default: 
     break; 
    } 
    return true; 
} 
} 

任何想法?我很樂意感謝你的幫助。謝謝。

回答

1

我認爲你最好的選擇是手動跟蹤每個對象的拖放狀態,方法是讓布爾值最初爲false,然後在拖放操作完成時將其設置爲true。然後檢查它們是否全部被刪除,只要構造一個簡單的if語句來檢查所有的布爾值是否爲真。

集類變量:

boolean choice1Dropped = false; 
boolean choice2Dropped = false; 
boolean choice3Dropped = false; 

然後在DragEvent.ACTION_DROP,設置相應的布爾爲true。 然後檢查它們是否全部掉線,只需說:

if (choice1Dropped && choice2Dropped && choice3Dropped) { 
    Toast.makeText(getApplicationContext(), "All objects have been dropped", Toast.LENGTH_LONG).show(); 
} 
+0

你能給出一些樣例實現嗎?因爲我有點困惑謝謝。 – Dunkey

+0

@Dunkey查看我的編輯 – Ogen

+0

我跟着這個我怎麼能設置choice1Dropped爲真如果我拖動choice1? – Dunkey