Android检查是否删除了所有对象

时间:2014-03-01 10:11:40

标签: android

大家好,我正在做一个拖放应用程序。我完成了拖放部分,现在我想做的是检查所有对象/项目是否已经放到了放置目标上并显示Toast消息,通知所有对象都被丢弃。

我该怎么做?这是我到目前为止所尝试的内容:

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 个答案:

答案 0 :(得分: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();
}