如何在Android中拖放EditText的内容?

时间:2015-08-26 15:26:38

标签: android android-layout android-fragments drag-and-drop

简而言之,我想让用户能够拖放EditText - 但是,当EditText被释放而不是丢弃时,由于某种原因它会消失,我收到消息:< / p>

I/ViewRootImpl﹕ Reporting drop result: false

我有一个带有2个FrameLayout&#39的活动,camera_preview和bottom_toolbar,如下所示:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/camera_preview"
        android:layout_above="@+id/bottom_toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <FrameLayout
        android:id="@+id/bottom_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_alignParentBottom="true">

    </FrameLayout>
</RelativeLayout>

当点击bottom_toolbar时,它会被片段替换,片段的XML包含一个包含2个EditTexts的LinearLayout。我在onCreate()中为这些EditText设置了触摸和拖动侦听器,并将这些类添加到了所述片段中:

private final class MyTouchListener implements View.OnTouchListener {
    public boolean onTouch(View view, MotionEvent motionEvent) {
        if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
            ClipData data = ClipData.newPlainText("", "");
            View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
            view.setVisibility(View.INVISIBLE);
            view.startDrag(data, shadowBuilder, view, 0);
            return true;
        }
        else
        {
            return false;
        }
    }
}

class MyDragListener implements View.OnDragListener {
    private android.widget.LinearLayout.LayoutParams layoutParams;

    @Override
    public boolean onDrag(View v, DragEvent event) {
        switch (event.getAction()) {
            case DragEvent.ACTION_DRAG_STARTED:
                layoutParams = (LinearLayout.LayoutParams) v.getLayoutParams();
                Log.d("TAG", "Started");
            break;
            case DragEvent.ACTION_DRAG_ENTERED:
                int x_cord = (int) event.getX();
                int y_cord = (int) event.getY();
                Log.d("TAG", "Entered");
                break;
            case DragEvent.ACTION_DRAG_EXITED:
                x_cord = (int) event.getX();
                y_cord = (int) event.getY();
                layoutParams.leftMargin = x_cord;
                layoutParams.topMargin = y_cord;
                v.setLayoutParams(layoutParams);
                Log.d("TAG", "Exited");
                break;

            case DragEvent.ACTION_DRAG_LOCATION:
                x_cord = (int) event.getX();
                y_cord = (int) event.getY();
                Log.d("TAG", "Loc");
                break;

            case DragEvent.ACTION_DRAG_ENDED:
                //((View)event.getLocalState()).setVisibility(View.VISIBLE);
                v.setVisibility(View.VISIBLE);
                Log.d("TAG", "Ended");
                break;

            case DragEvent.ACTION_DROP:
                v.setVisibility(View.VISIBLE);
                Log.d("TAG", "Dropped");
                break;
            default:

            break;
        }
        return true;
    }

}  

有人知道为什么我的EditText在丢弃时消失了吗?我花了无数个小时在线寻找解决方案,但它们似乎都没有用。

感谢任何建议,谢谢。

编辑:

将我的拖动侦听器更改为:

class MyDragListener implements View.OnDragListener {
        @Override
        public boolean onDrag(View v, DragEvent event) {
            switch (event.getAction()) {
                case DragEvent.ACTION_DRAG_STARTED:
                    break;
                case DragEvent.ACTION_DRAG_ENTERED:
                    break;
                case DragEvent.ACTION_DRAG_EXITED:
                    break;
                case DragEvent.ACTION_DROP:
                    View view = (View) event.getLocalState();
                    ViewGroup owner = (ViewGroup) view.getParent();
                    owner.removeView(view);
                    FrameLayout container = (FrameLayout) v;
                    container.addView(view);
                    view.setVisibility(View.VISIBLE);
                    break;
                case DragEvent.ACTION_DRAG_ENDED:
                default:
                    break;
            }
            return true;
        }
    }

我从我的EditText中删除了拖动侦听器(保留了触摸侦听器)并将其添加到我想要删除它们的视图中。我不再收到错误的丢弃消息,并且我确定它会丢弃EditText,因为当它们被拖出时片段会变小。我仍然无法看到他们。可能是他们被放弃在视线之外而不是在它前面?如果是这样,我该如何避免呢?再次感谢。

编辑:碰撞

0 个答案:

没有答案