android edittext拖放问题

时间:2014-03-28 12:06:43

标签: android drag-and-drop android-edittext ontouchlistener android-gesture

我正在开发一个照片编辑器应用程序,我正在尝试拖放一个edittext,用户可以在其中输入任何字符串。我已经应用了触摸监听器来拖动视图,它第一次完美运行但是当我第二次拖放edittext时,当用户放下视图时它将不可见。

这是我的代码段:

这是我的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());

请看看并建议我如何解决它。 提前谢谢。

1 个答案:

答案 0 :(得分:-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;
        }
     });