Create EditText by drag drop Android issue

落花浮王杯 提交于 2019-12-25 06:48:05

问题


I have three layouts: top, left and right. On top layout, I have EditText icon, when I drag it to left layout, will create an EditText in left layout.

With EditText drop in left layout or right layout, I can move it from left to right layout (or right to left, can't move from left/right to top)

My problem:

1.When I create an EditText by drag in left layout, then I create an EditText by drag in left layout, it work's well, I can see all drag drop log action. But if I create another EditText in left layout again, ACTION_DRAG_STARTED and ACTION_DRAG_ENDED not received.

http://i1057.photobucket.com/albums/t394/tdtrinhsiho/ScreenShot2013-07-28at60954PM_zps14a2d784.png

2.When I create an EditText too close EditText already created in layout, ACTION_DROP not received

http://i1057.photobucket.com/albums/t394/tdtrinhsiho/ScreenShot2013-07-28at61336PM_zps98ab85f1.png

My code: XML:

    <LinearLayout
            android:id="@+id/top"
            android:layout_width="fill_parent"
            android:layout_height="100dip"
            android:background="@color/Azure">

        <ImageView
                android:id="@+id/sticker"
                android:padding="10dip"
                android:layout_width="50dip"
                android:layout_height="50dip"
                android:background="@drawable/note"
                >

        </ImageView>

    </LinearLayout>

    <LinearLayout
            android:layout_height="fill_parent"
            android:layout_width="fill_parent"
            android:orientation="horizontal">

        <AbsoluteLayout
                android:id="@+id/left"
                android:layout_width="500dip"
                android:layout_height="fill_parent"
                android:background="@color/OldLace">

        </AbsoluteLayout>

        <AbsoluteLayout
                android:id="@+id/right"
                android:layout_height="fill_parent"
                android:layout_width="fill_parent"
                android:background="@color/BurlyWood">

        </AbsoluteLayout>

    </LinearLayout>

</LinearLayout>

Activity: package com.tranductrinh.mydraganddrop;

import android.annotation.TargetApi;
import android.app.ActionBar;
import android.content.ClipData;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.DragEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewGroup;
import android.widget.AbsoluteLayout;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

public class Main extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        findViewById(R.id.sticker).setOnTouchListener(new MyTouchListener());
        findViewById(R.id.left).setOnDragListener(new MyDragListener());
        findViewById(R.id.right).setOnDragListener(new MyDragListener());

    }

    private 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.startDrag(data, shadowBuilder, view, 0);
                //view.setVisibility(View.INVISIBLE);
                return true;
            } else
                return false;
        }
    }

    private  class MyLongTouchListener implements View.OnLongClickListener {
        public boolean onLongClick(View view) {
            ClipData data = ClipData.newPlainText("", "");
            View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
            view.startDrag(data, shadowBuilder, view, 0);
            //view.setVisibility(View.INVISIBLE);
            return true;
        }
    }

    class MyDragListener implements View.OnDragListener {            

        @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
        @Override
        public boolean onDrag(View v, DragEvent event) {
            int action = event.getAction();

            switch (event.getAction()) {
                case DragEvent.ACTION_DRAG_STARTED:

                    Log.i("INFORMATION ", "START");
                    return true;
                case DragEvent.ACTION_DRAG_ENTERED:

                    v.setBackgroundColor(getResources().getColor(R.color.YellowGreen));
                    Log.i("INFORMATION ", "ENTER");
                    return true;
                case DragEvent.ACTION_DRAG_EXITED:

                    if (v == findViewById(R.id.left))
                        v.setBackgroundColor(getResources().getColor(R.color.OldLace));
                    else
                        if (v == findViewById(R.id.right))
                            v.setBackgroundColor(getResources().getColor(R.color.BurlyWood));

                    Log.i("INFORMATION ", "EXIT");

                    return true;
                case DragEvent.ACTION_DROP:
                    // Dropped, reassign View to ViewGroup
                    Log.i("INFORMATION ", "DROP");
                    View view = (View) event.getLocalState();
                    ViewGroup owner = (ViewGroup) view.getParent();

                    if (owner.getId() != R.id.top) {
                        // remove Sticker in from layout
                        owner.removeView(view);

                        // add Sticker to new layout
                        AbsoluteLayout container = (AbsoluteLayout) v;
                        AbsoluteLayout.LayoutParams params = new AbsoluteLayout.LayoutParams(200, 200, (int)event.getX()-100, (int)event.getY()-100);
                        container.addView(view, params);
                        view.setVisibility(View.VISIBLE);
                        view.setFocusable(false);

                        // set onLongClickListener
                        view.setOnLongClickListener(new MyLongTouchListener());

                        Log.i("INFORMATION ", "Move operation");

                    }
                    else if (owner.getId() == R.id.top) {

                        //view.setVisibility(View.VISIBLE);

                        // new EditText object by code
                        EditText editText = new EditText(Main.this);
                        editText.setBackgroundDrawable(getResources().getDrawable(R.drawable.note));

                        // add Sticker to Container
                        AbsoluteLayout container = (AbsoluteLayout) v;
                        AbsoluteLayout.LayoutParams params = new AbsoluteLayout.LayoutParams(200, 200, (int)event.getX()-100, (int)event.getY()-100);
                        container.addView(editText, params);

                        // set focus on EditText
                        editText.setFocusable(false);

                        // set onLongClickListener
                        editText.setOnLongClickListener(new MyLongTouchListener());

                        Log.i("INFORMATION ", "Copy operation");

                    }

                    // set view parent to normal
                    if (v == findViewById(R.id.left)) {
                        v.setBackgroundColor(getResources().getColor(R.color.OldLace));
                    }
                    else
                    if (v == findViewById(R.id.right)) {
                        v.setBackgroundColor(getResources().getColor(R.color.BurlyWood));
                    }

                    return true;

                case DragEvent.ACTION_DRAG_ENDED:
                    Log.i("INFORMATION ", "Drag Drop Result : " + event.getResult());

                    if (event.getResult() == false) {

                        Toast toast = Toast.makeText(Main.this, "Invalid operation drag and drop", Toast.LENGTH_SHORT);
                        toast.show();
                        // when drop action not handler
                        ((View) event.getLocalState()).setVisibility(View.VISIBLE);
                    }

                    Log.i("INFORMATION ", "END *******************");

                    return true;

                default:
                    break;
            }
            return false;
        }
    }


}

回答1:


Instead of moving the EditText, you should make a copy of it in the view. For example, you should do the following:

String editTextString = editText.getText().toString();
EditText newEditText = new EditText(MainActivity.this);
newEditText.setText(editTextString);

So, you need to copy the text from the EditText and create a new EditText which you will add to the container. Make sure that you don't make the view invisible when you call v.startDrag(data, shadowBuilder, v, 0);



来源:https://stackoverflow.com/questions/17907703/create-edittext-by-drag-drop-android-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!