我正在开发一个照片编辑器应用程序,其中我试图拖放一个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());


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

最佳答案

使用此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;
        }
     });

10-07 19:14
查看更多