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

請看看,並建議我如何解決它。 在此先感謝。

+0

把在DragEvent.ACTION_DRAG_ENDED情況下下降的代碼,而不是案件DragEvent.ACTION_DROP。它會工作 – TheLittleNaruto

回答

-1

使用本MotionEvent.ACTION_MOVE我不知道編輯文字工作或沒有,但我的做工精細在這裏看到的情況下,圖像是完整的代碼

Android Drag and drop images on the Screen?

public boolean onTouch(View v, MotionEvent event) { 
    layoutParams = (RelativeLayout.LayoutParams) ima1.getLayoutParams(); 

    switch(event.getAction())     
     { 
      case MotionEvent.ACTION_DOWN:       
       break;  

      case MotionEvent.ACTION_MOVE: 
       int x_cord = (int) event.getRawX(); 
       int y_cord = (int) event.getRawY(); 

      System.out.println("value of x" +x_cord); 
      System.out.println("value of y" +y_cord);   

       if (x_cord > windowwidth) { 
        x_cord = windowwidth; 
        } 
       if (y_cord > windowheight) { 
        y_cord = windowheight; 
        } 
     layoutParams.leftMargin = x_cord-25; 
     layoutParams.topMargin = y_cord-25; 
     // layoutParams.rightMargin = x_cord-25; 
     // layoutParams.bottomMargin = y_cord-25; 
     ima1.setLayoutParams(layoutParams); 
       break; 
      default: break; 
      } 
      return true; 
     } 
    });