DragandDrop.java
import android.content.Intent;
import android.os.Bundle;
import android.view.DragEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.DragShadowBuilder;
import android.view.View.OnDragListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.app.Activity;
public class DragandDrop extends Activity implements OnTouchListener, OnDragListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.draganddrop);
findViewById(R.id.squareImage).setOnTouchListener(this);
findViewById(R.id.circleImage).setOnTouchListener(this);
findViewById(R.id.triangleImage).setOnTouchListener(this);
findViewById(R.id.squareImage1).setOnDragListener(this);
findViewById(R.id.circleImage1).setOnDragListener(this);
findViewById(R.id.triangleImage1).setOnDragListener(this);
}
@Override
public boolean onTouch(View v, MotionEvent e) {
if (e.getAction() == MotionEvent.ACTION_DOWN) {
DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);
v.startDrag(null, shadowBuilder, v, 0);
v.setVisibility(View.INVISIBLE);
return true;
} else {
return false;
}
}
@Override
public boolean onDrag(View v, DragEvent e) {
if (e.getAction()==DragEvent.ACTION_DROP) {
View view = (View) e.getLocalState();
if(view.getId()==R.id.squareImage && v.getId()==R.id.squareImage1)
{
ViewGroup from = (ViewGroup) view.getParent();
from.removeView(view);
v.setBackgroundResource(R.drawable.dragsquare1);
return true;
} else if(view.getId()==R.id.circleImage && v.getId()==R.id.circleImage1){
ViewGroup from = (ViewGroup) view.getParent();
from.removeView(view);
v.setBackgroundResource(R.drawable.dragcircle1);
return true;
} else if(view.getId()==R.id.triangleImage && v.getId()==R.id.triangleImage1){
ViewGroup from = (ViewGroup) view.getParent();
from.removeView(view);
v.setBackgroundResource(R.drawable.dragtriangle1);
return true;
}
else {
return true;
}
}
return true;
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.reset:
Intent intent = getIntent();
finish();
startActivity(intent);
}
}
}
答案 0 :(得分:0)
摆脱说v.setVisibility(View.INVISIBLE)的行;在你的onTouch方法中。 按下时,会使视图不可见。