gpt4 book ai didi

java - Android,拖放后删除拖动的项目

转载 作者:行者123 更新时间:2023-12-01 14:47:59 26 4
gpt4 key购买 nike

// Touched
public class ChoiceTouchListener implements OnTouchListener {
public boolean onTouch(View view, MotionEvent motionEvent) {

switch (motionEvent.getAction()) {

case MotionEvent.ACTION_MOVE:

return true;

case MotionEvent.ACTION_DOWN:
Viewed = String.valueOf(view.getId());

ClipData data = ClipData.newPlainText("", "");
DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);

// Drag Function When Item Touched
view.startDrag(data, shadowBuilder, view, 0);
return true;

case MotionEvent.ACTION_UP:
return true;

default:
return true;
}
}
}



private class ChoiceDragListener implements OnDragListener {

// onDrag Method, imagine we dragging numbers from display TextView
@Override
public boolean onDrag(View v, DragEvent event) {

// Android has onDrag action types. there are some kind of action
switch (event.getAction()) {

// if drag stared
case DragEvent.ACTION_DRAG_STARTED:

break;

// if drag entered software
case DragEvent.ACTION_DRAG_ENTERED:

break;

// if drag exited, stopped or something
case DragEvent.ACTION_DRAG_EXITED:

break;

// main case, if drag dropped what we do...
case DragEvent.ACTION_DROP:

// handle the dragged view being dropped over a drop view
View view = (View) event.getLocalState();

// stop displaying the view where it was before it was dragged
// ************* view.setVisibility(View.KEEP_SCREEN_ON);

// view dragged item, where numbers will be dragged
dropTarget = (TextView) v;

// view dropped item, that will be dropped in drag TextView
dropped = (TextView) view;

// view result place, when make math operation, show results
resultPlace = (TextView) findViewById(R.id.calcResult);


// simple debug
// ****** resultPlace.setText( dropped.getText() + " " + dropTarget.getText() );

// 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());

// show results
resultPlace.setText( dropped.getText() + " " + dropTarget.getText() + " " + dropped.getText() );



break;

// if drag ended, we did it already successfully
case DragEvent.ACTION_DRAG_ENDED:

break;

// what do default
default:

break;
}
return true;
}
}

我有非常简单的触摸和拖动类。我唯一的问题是,当我将项目拖放到特定布局中时,我的项目也保持在旧位置。 android 中有没有什么方法可以检测我正在触摸的项目并在掉落后将其删除?

我有一个小型应用程序,单击数字并在仪表板中拖放,然后重复此操作并将仪表板中的所有项目拖动到 X 和 Y 位置。这就是为什么我想清空旧号码空间来创建另一个号码空间。

谢谢...

最佳答案

我最终在我的程序中得到了类似的东西,希望它有所帮助。

View.OnDragListener DropListener = new View.OnDragListener() {
@Override
public boolean onDrag(View v, DragEvent event) {
int dragEvent = event.getAction();

switch (dragEvent) {
case DragEvent.ACTION_DRAG_ENTERED:
Log.i("Drag Event", "Entered");
break;

case DragEvent.ACTION_DRAG_EXITED:
Log.i("Drag Event", "Exited");
break;

case DragEvent.ACTION_DROP:
TextView target = (TextView) v;
TextView dragged = (TextView) event.getLocalState();
target.setText((CharSequence) dragged.getText());
dragged.setText("");
break;

}
return true;
}
};

关于java - Android,拖放后删除拖动的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15212682/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com