我正在使用拖放开发Android应用程序。当我拖动一个按钮。然后我想按名称识别按钮(获取按钮名称)。我想在DragEvent.ACTION_DROP中获取按钮名称。
我的代码如下所示。
drop.setOnDragListener(new View.OnDragListener() {
@Override
public boolean onDrag(View v, DragEvent event) {
//return false;
View dragView = (View) event.getLocalState();
final int action = event.getAction();
switch(action) {
case DragEvent.ACTION_DRAG_STARTED:
//Log.d("LockView", "ACTION_DRAG_STARTED");
break;
case DragEvent.ACTION_DRAG_EXITED:
//Log.d("LockView", "ACTION_DRAG_EXITED");
break;
case DragEvent.ACTION_DRAG_ENTERED:
//Log.d("LockView", "ACTION_DRAG_ENTERED");
break;
case DragEvent.ACTION_DROP:{
failure = failure+1;
Log.d("LockView", "ACTION_DROP");
return(true);
}
case DragEvent.ACTION_DRAG_ENDED:{
//Log.d("LockView", "ACTION_DRAG_ENDED");
return(true);
}
default:
break;
}
return true;
}
我的按钮代码是
btn1.setOnTouchListener(new OnTouchListener() {@Override public boolean onTouch(View v, MotionEvent event) {
Log.d("LOGTAG", "Touched c"+c);
ClipData data = ClipData.newPlainText("", "");
View.DragShadowBuilder shadow = new View.DragShadowBuilder(btn1);
v.startDrag(data, shadow, v, 0);
Log.d("LOGTAG", "Touched");
if(SystemClock.elapsedRealtime() - c < 1000) {
if (position > -1 && position < 4) {
nums[position] = 1;
}
setPosition(position);
}
c=SystemClock.elapsedRealtime();
// TODO Auto-generated method stub
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
startX = event.getX();
startY = event.getY();
break;
case MotionEvent.ACTION_UP:
float endX = event.getX();
float endY = event.getY();
if (isAClick(startX, endX, startY, endY)) {
Log.d("LockView", "clicked");
/*if (position > -1 && position < 4) {
nums[position] = 1;
}
setPosition(position);*/
} else {
}
break;
}
v.getParent().requestDisallowInterceptTouchEvent(true); //specific to my project
return false;
}
});
当我拖动按钮btn1时。然后我想确定按钮。