2014-03-01 2 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

나는 당신의 최선의 선택은 수동으로 부울 값을 초기 값으로 설정 한 다음 드롭 작업이 완료되면 true로 설정하여 각 개체의 드롭 상태를 수동으로 추적하는 것이라고 생각합니다. 그런 다음 모두 삭제되었는지 확인하려면 간단한 boolean 문이 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를 true로 설정할 수 있습니까? – Dunkey

관련 문제