1
我正在一個照片編輯器應用程序,其中我試圖拖放一個edittext,其中用戶可以輸入任何字符串。我已應用觸摸偵聽器拖動視圖,它運行第一次完美,但是當我第二次拖放edittext時,它在用戶放下視圖時變得不可見。android編輯文本拖放問題
這裏是我的代碼片段:
這是我EDITTEXT
<RelativeLayout
android:layout_below="@+id/top"
android:id="@+id/middle1"
android:layout_width="wrap_content"
android:layout_height="420dp"
>
<EditText
android:id="@+id/screen"
android:layout_width="200dp"
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:background="@drawable/screen" />
</RelativeLayout>
這是java代碼
private final class MyTouchListener implements OnTouchListener {
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
ClipData data = ClipData.newPlainText("", "");
DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
view.startDrag(data, shadowBuilder, view, 0);
view.setVisibility(View.INVISIBLE);
return true;
} else {
return false;
}
}
}
class MyDragListener implements OnDragListener {
@Override
public boolean onDrag(View v, DragEvent event) {
int action = event.getAction();
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
// do nothing
break;
case DragEvent.ACTION_DRAG_ENTERED:
break;
case DragEvent.ACTION_DRAG_EXITED:
break;
case DragEvent.ACTION_DROP:
// Dropped, reassign View to ViewGroup
if(v instanceof ViewGroup && ((ViewGroup)v).getChildCount()!=0){
View view = (View) event.getLocalState();
ViewGroup owner = (ViewGroup) view.getParent();
owner.removeView(view);
RelativeLayout container = (RelativeLayout) v;
container.addView(view);
view.setVisibility(View.VISIBLE);}
break;
case DragEvent.ACTION_DRAG_ENDED:
default:
break;
}
return true;
}
}
這該是多麼我已經應用觸摸和拖拽監聽器:
findViewById(R.id.screen).setOnTouchListener(new MyTouchListener());
findViewById(R.id.middle1).setOnDragListener(new MyDragListener());
請看看,並建議我如何解決它。 在此先感謝。
把在DragEvent.ACTION_DRAG_ENDED情況下下降的代碼,而不是案件DragEvent.ACTION_DROP。它會工作 – TheLittleNaruto