拖放动态添加的editText

时间:2016-06-05 13:28:51

标签: android dynamic drag-and-drop android-edittext

我有一个按钮,点击后会添加一个新的editText。这些动态添加的editTexts必须可以拖放到屏幕上的任何位置。但是当单击按钮时,会添加editText,但无法输入任何内容。它仍然可以在任何地方拖放 现在当我点击按钮添加另一个editText时仍然是一样的;无法键入,但拖放时,前一个edittext会消失。 这种情况一直在发生。但是当我尝试使用textViews而不是editTexts时,它工作正常。

这就是textview的工作方式,我希望与editTexts类似

VIDEO

我的主要活动

public class MainActivity extends Activity 
{

RelativeLayout dropLayout;
LayoutParams params;
Button addEdittextBtn;

@Override
protected void onCreate(Bundle savedInstanceState)
{

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    dropLayout = (RelativeLayout) findViewById(R.id.ondraglayout);  
    dropLayout.setOnDragListener(new MyDragListener());
    addEdittextBtn = (Button) findViewById(R.id.addET);

    addEdittextBtn.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View p1)
            {
                EditText et = new EditText (MainActivity.this);
                et.setText("DRAG ME");
                dropLayout.addView(et,);
                et.setOnTouchListener(new MyTouchListener());
            }


        });

    }

}

MyTouchListener类

public class MyTouchListener implements OnTouchListener
{

    @Override
    public boolean onTouch(View v, MotionEvent event)
    {

            ClipData dragdata = ClipData.newPlainText("","");

            View.DragShadowBuilder shdwbldr = new View.DragShadowBuilder(v);

            v.startDrag(dragdata, shdwbldr, v, 0);
            v.setVisibility(View.INVISIBLE);

            return true;
    }


}

mydraglistener类

public class MyDragListener implements OnDragListener
{

private RelativeLayout.LayoutParams params;

@Override
public boolean onDrag(View v, DragEvent event)
{
    View view = (View) event.getLocalState();


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

            params = (RelativeLayout.LayoutParams) view.getLayoutParams();  
            break;

        case DragEvent.ACTION_DRAG_ENTERED:
            int x = (int) event.getX();
            int y = (int) event.getY();

            break;

        case DragEvent.ACTION_DRAG_EXITED :

            break;

        case DragEvent.ACTION_DRAG_LOCATION  :
            x=  (int) event.getX();
            y =  (int) event.getY();
            break;

        case DragEvent.ACTION_DRAG_ENDED   :

            break;

        case DragEvent.ACTION_DROP:

            x = (int) event.getX();
            y = (int) event.getY();
            params.leftMargin = x;
            params.topMargin = y;

            view.setLayoutParams(params);
            view.setVisibility(View.VISIBLE);



            break;
        default: break;
    }
    return true;
}


}

0 个答案:

没有答案