Android:删除之后,被拖动的editText消失了

时间:2015-11-12 04:34:13

标签: android android-studio drag-and-drop android-emulator relativelayout

我正在尝试创建一个用户可以移动输入的表单。我有一个editText,希望它能够将它移动到我的relativeLayout中的新位置。我能够把它拿起并移动它,但是一旦我放手,它就完全消失了。我已经尝试了很多东西。我认为可能永远不会调用onDrag方法,因为我的日志永远不会出现。另外,在放弃我的editText后,我在logcat中收到此错误消息:Reporting drop result: false

我遇到的另一个问题是,当你点击editText时,我认为我试图拖动它意味着我实际上无法更改其中的文字。

任何帮助表示赞赏!谢谢!

这是我的相对布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android"
                xmlns:app = "http://schemas.android.com/apk/res-auto"
                xmlns:tools = "http://schemas.android.com/tools"
                android:layout_width = "match_parent"
                android:layout_height = "match_parent"
                android:paddingBottom = "@dimen/activity_vertical_margin"
                android:paddingLeft = "@dimen/activity_horizontal_margin"
                android:paddingRight = "@dimen/activity_horizontal_margin"
                android:paddingTop = "@dimen/activity_vertical_margin"
                app:layout_behavior = "@string/appbar_scrolling_view_behavior"
                tools:context = "com.supercilex.myapp.NewForm"
                tools:showIn = "@layout/activity_new_form"
    android:id="@+id/relativeLayout">

    <EditText
        android:id = "@+id/editText"
        android:layout_width = "wrap_content"
        android:layout_height = "wrap_content"
        android:layout_alignParentStart = "true"
        android:layout_alignParentTop = "true"
        android:ems = "10"
        android:hint = "Enter some text"
        android:inputType = "textPersonName" />
</RelativeLayout>

这是我的班级:

import android.content.ClipData;
import android.content.ClipDescription;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.DragEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;

import common.FileManagement;

public class NewForm extends AppCompatActivity
{
    private static EditText editText;
    private android.widget.RelativeLayout.LayoutParams layoutParams;
    String msg;

    @Override
    protected void onCreate ( Bundle savedInstanceState )
    {
        super.onCreate ( savedInstanceState );
        setContentView ( R.layout.activity_new_form );
        Toolbar toolbar = ( Toolbar ) findViewById ( R.id.toolbar );
        setSupportActionBar ( toolbar );
        editText = ( EditText ) findViewById ( R.id.editText );
        FileManagement fileManagement = new FileManagement ();

        editText.setText ( fileManagement.read ( "textViewData", getApplicationContext () ) );

        FloatingActionButton fab = ( FloatingActionButton ) findViewById ( R.id.fab );
        fab.setOnClickListener ( new View.OnClickListener ()
        {
            @Override
            public void onClick ( View view )
            {
                FileManagement fileManagement = new FileManagement ();
                String tmpEditText = editText.getText ().toString ();
                MainActivity.textView.setText ( tmpEditText );
                fileManagement.save ( "textViewData", tmpEditText, getApplicationContext () );
                finish ();
            }
        } );

        editText.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                ClipData.Item item = new ClipData.Item((CharSequence)v.getTag());
                String[] mimeTypes = { ClipDescription.MIMETYPE_TEXT_PLAIN};

                ClipData dragData = new ClipData(v.getTag().toString(),mimeTypes, item);
                View.DragShadowBuilder myShadow = new View.DragShadowBuilder(editText);

                v.startDrag(dragData,myShadow,null,0);
                return true;
            }
        });

        editText.setOnDragListener(new View.OnDragListener() {
            @Override
            public boolean onDrag(View v, DragEvent event) {
                switch(event.getAction())
                {
                    case DragEvent.ACTION_DRAG_STARTED:
                        layoutParams = (RelativeLayout.LayoutParams)v.getLayoutParams();
                        Log.d(msg, "Action is DragEvent.ACTION_DRAG_STARTED");
                        return true;

                    case DragEvent.ACTION_DRAG_ENTERED:
                        Log.d(msg, "Action is DragEvent.ACTION_DRAG_ENTERED");
                        int x_cord = (int) event.getX();
                        int y_cord = (int) event.getY();
                        return true;

                    case DragEvent.ACTION_DRAG_EXITED :
                        Log.d(msg, "Action is 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);
                        return true;

                    case DragEvent.ACTION_DRAG_LOCATION  :
                        Log.d(msg, "Action is DragEvent.ACTION_DRAG_LOCATION");
                        x_cord = (int) event.getX();
                        y_cord = (int) event.getY();
                        return true;

                    case DragEvent.ACTION_DRAG_ENDED   :
                        Log.d ( msg, "Action is DragEvent.ACTION_DRAG_ENDED" );

                        return true;

                    case DragEvent.ACTION_DROP:
                        Log.d( msg, "ACTION_DROP event");
                        return true;
                    default:
                        break;
                }
                return true;
            }
        });

        editText.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    ClipData data = ClipData.newPlainText("", "");
                    View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(editText);

                    editText.startDrag(data, shadowBuilder, editText, 0);
                    editText.setVisibility(View.INVISIBLE);
                    return true;
                }
                else
                {
                    return false;
                }
            }
        });
    }
}

我还尝试使用break;代替return true;的onDrag方法 我已经这样试过了:

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_DRAG_LOCATION  :
                        break;

                    case DragEvent.ACTION_DRAG_ENDED   :
                        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);
                        Log.d( msg, "ACTION_DROP event");
                        break;
                    default:
                        break;
                }
                return true;
            }

修改 这是我更新的代码:

高于onCreate()

private EditText editText;
private RelativeLayout.LayoutParams layoutParams; 

其余的:

findViewById(R.id.yourlayoutname).setOnDragListener( new View.OnDragListener()
            {
                @Override
                public boolean onDrag ( View v, DragEvent event )
                {
                    if ( event.getAction() == DragEvent.ACTION_DROP )
                    {
                        int xCord = ( int ) event.getX();
                        int yCord = ( int ) event.getY();
                        int height = editText.getHeight();
                        int width = editText.getWidth();
                        layoutParams.leftMargin = xCord - width;
                        layoutParams.topMargin = yCord - height;
                        editText.setLayoutParams( layoutParams );
                    }
                    return true;
                }
            });

            editText.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    String[] mimeTypes = { ClipDescription.MIMETYPE_TEXT_PLAIN};

                    ClipData data = ClipData.newPlainText("", "");
                    View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(editText);

                    v.startDrag( data, shadowBuilder, mimeTypes, 0);
                    return true;
                }
            } );

            editText.setOnDragListener( new View.OnDragListener()
            {
                @Override
                public boolean onDrag ( View v, DragEvent event )
                {
                    if ( event.getAction() == DragEvent.ACTION_DRAG_STARTED )
                    {
                        layoutParams = ( RelativeLayout.LayoutParams ) v.getLayoutParams();
                    }
                    return true;
                }
            } );

1 个答案:

答案 0 :(得分:1)

I get this error message in logcat: Reporting drop result: false 这不是错误消息。这意味着true View的行动结果未返回DragListener

您的EditText消失了,因为您将View.INVISIBLE内的onTouchListener设置为editText.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { ClipData data = ClipData.newPlainText("", ""); View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(editText); editText.startDrag(data, shadowBuilder, editText, 0); editText.setVisibility(View.INVISIBLE); return true; } else { return false; } } }); 并且从未更改过。

试试这个: 首先选择是否要通过触摸或长按拖动。你不需要两者兼而有之。让我们说你决定通过触摸来做到这一点:

EditText

从代码侦听器中删除所有reduntant代码,不需要它来拖动,因为系统会为您处理拖动。如果您希望Drop的协调成为DragListener的新位置,则需要将EditText分配给RelativeLayout容器 editText.setOnDragListener(new View.OnDragListener() { @Override public boolean onDrag(View v, DragEvent event) { if (event.getAction == DragEvent.ACTION_DRAG_STARTED){ layoutParams = (RelativeLayout.LayoutParams)v.getLayoutParams(); } else if (event.getAction() == DragEvent.ACTION_DRAG_ENDED) v.setVisibility(View.VISIBLE) return true; } }); ,并在`ACTION_DROP中检索它们。如果不是:

findViewById(R.id.relativeLayout).setOnDragListener(new View.OnDragListener() {
    @Override
    public boolean onDrag(View v, DragEvent event) {
        if (event.getAction() ==   DragEvent.ACTION_DRAG_DROP){
             int x_cord = (int) event.getX();
             int  y_cord = (int) event.getY();
             layoutParams.leftMargin = x_cord;
             layoutParams.topMargin = y_cord;
             editText.setLayoutParams(layoutParams);

        }
        return true;
    }
});

要获得放置位置,您需要类似的东西:

Xcode