将TextView的文本拖放到TextView

时间:2014-12-29 12:00:47

标签: android drag-and-drop

我正在尝试使用拖放功能制作应用,但我无法将文本从textview拖动到textview,但是从文本视图拖动到使用此代码编辑文本。

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class MainActivity extends Activity implements OnLongClickListener {

private TextView textView;
private TextView textView1;

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    textView = (TextView) findViewById(R.id.textView);

    myDragEventListener mDragListen = new myDragEventListener();

    textView.setTag("Example");

    textView.setOnLongClickListener(this);

    textView1 = (TextView)findViewById(R.id.textView1);

    textView1.setOnDragListener(mDragListen);
}


protected class myDragEventListener implements View.OnDragListener
{

    @Override
    public boolean onDrag(View v, DragEvent event) 
    {
        final int action = event.getAction();

        switch(action) 
        {

        case DragEvent.ACTION_DRAG_STARTED:

            if (event.getClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) 
            {

                return true;
            }

            // Returns false. During the current drag and drop operation, this View will
            // not receive events again until ACTION_DRAG_ENDED is sent.
            return false;

        case DragEvent.ACTION_DRAG_ENTERED:


            return true;

        case DragEvent.ACTION_DRAG_LOCATION:

            // Ignore the event
            return true;

        case DragEvent.ACTION_DRAG_EXITED:


            return true;

        case DragEvent.ACTION_DROP:

            if(v == findViewById(R.id.textView1)) 
            {
                TextView view = (TextView) event.getLocalState();
                textView1.setText(view.getText().toString());
            } 
            else
            {
                View view = (View) event.getLocalState();
                view.setVisibility(View.VISIBLE);
                Toast.makeText(MainActivity.this, "You can't drop the image here", Toast.LENGTH_LONG).show();
                break;
            }
            break;

        case DragEvent.ACTION_DRAG_ENDED:

            v.invalidate();

            // Does a getResult(), and displays what happened.
            if (event.getResult())
            {
                Toast.makeText(MainActivity.this, "The drop was handled.", Toast.LENGTH_LONG).show();
            }
            else 
            {

            }

            // returns true; the value is ignored.
            return true;

            // An unknown action type was received.
        default:
            Log.e("DragDrop Example","Unknown action type received by OnDragListener.");
            break;
        }

        return false;
    }
}


@Override
public boolean onLongClick(View view)
{
    TextView textView = (TextView) view;
    ClipData.Item item = new ClipData.Item(textView.getText().toString());
    String[] mimeTypes = { ClipDescription.MIMETYPE_TEXT_PLAIN };
    ClipData data = new ClipData("", mimeTypes, item);
    DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);

    view.startDrag( data,
            shadowBuilder,//data to be dragged
            view, //local data about the drag and drop operation
            0   //no needed flags
            );

    return true;
};
}

我的错误在哪里?

1 个答案:

答案 0 :(得分:0)

而不是longClickListener(),请使用onTouchListener()。如果它不起作用,请告诉我

textView.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent arg1) {
                // TODO Auto-generated method stub
                ClipData data = ClipData.newPlainText("datalabel", "text");
                View.DragShadowBuilder shadow = new View.DragShadowBuilder(drag);
                v.startDrag(data, shadow, null, 0);
                return false;
            }
        });