通过拖放Android的问题创建的EditText [英] Create EditText by drag drop Android issue

查看:144
本文介绍了通过拖放Android的问题创建的EditText的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个布局:顶部,左,右。在上面的布局,我的EditText图标,当我把它拖动到左的布局,将创建左的布局一个EditText。

随着左布局或向右布局的EditText下降,我可以由左到右的布局移动(或从右到左,不能左/右顶部移动)

我的问题:

1。当我创建由左布局拖动一个EditText,然后创建在左侧布局拖动一个EditText,它的工作的很好,我可以看到所有的拖放操作日志。但是,如果我再创造左的布局另一个的EditText,ACTION_DRAG_STARTED和ACTION_DRAG_ENDED没有收到。

<一个href=\"http://i1057.photobucket.com/albums/t394/tdtrinhsiho/ScreenShot2013-07-28at60954PM_zps14a2d784.png\" rel=\"nofollow\">http://i1057.photobucket.com/albums/t394/tdtrinhsiho/ScreenShot2013-07-28at60954PM_zps14a2d784.png

2,当我创建的布局已经创建了一个EditText太近的EditText,ACTION_DROP没有收到

<一个href=\"http://i1057.photobucket.com/albums/t394/tdtrinhsiho/ScreenShot2013-07-28at61336PM_zps98ab85f1.png\" rel=\"nofollow\">http://i1057.photobucket.com/albums/t394/tdtrinhsiho/ScreenShot2013-07-28at61336PM_zps98ab85f1.png

我的code:
XML:
    

 &LT;的LinearLayout
            机器人:ID =@ + ID /顶
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =100dip
            机器人:背景=@色/ Azure的&GT;        &LT; ImageView的
                机器人:ID =@ + ID /贴纸
                机器人:填充=10dip
                机器人:layout_width =50dip
                机器人:layout_height =50dip
                机器人:背景=@绘制/笔记
                &GT;        &LT; / ImageView的&GT;    &LT; / LinearLayout中&GT;    &LT;的LinearLayout
            机器人:layout_height =FILL_PARENT
            机器人:layout_width =FILL_PARENT
            机器人:方向=横向&GT;        &LT; AbsoluteLayout
                机器人:ID =@ + ID /左
                机器人:layout_width =500dip
                机器人:layout_height =FILL_PARENT
                机器人:背景=@色/旧布黄&GT;        &LT; / AbsoluteLayout&GT;        &LT; AbsoluteLayout
                机器人:ID =@ + ID /右
                机器人:layout_height =FILL_PARENT
                机器人:layout_width =FILL_PARENT
                机器人:背景=@色/原木色&GT;        &LT; / AbsoluteLayout&GT;    &LT; / LinearLayout中&GT;&LT; / LinearLayout中&GT;

活动:
    包com.tranductrinh.mydraganddrop;

 进口android.annotation.TargetApi;
进口android.app.ActionBar;
进口android.content.ClipData;
进口android.graphics.Color;
进口android.graphics.drawable.Drawable;
进口android.os.Build;
进口android.os.Bundle;
进口android.app.Activity;
进口android.util.Log;
进口android.view.DragEvent;
进口android.view.LayoutInflater;
进口android.view.Menu;
进口android.view.MotionEvent;
进口android.view.View;
进口android.view.ViewGroup.LayoutParams;
进口android.view.ViewGroup;
进口android.widget.AbsoluteLayout;
进口android.widget.EditText;
进口android.widget.ImageView;
进口android.widget.LinearLayout;
进口android.widget.Toast;公共类主要活动扩展{    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
        findViewById(R.id.sticker).setOnTouchListener(新MyTouchListener());
        findViewById(R.id.left).setOnDragListener(新MyDragListener());
        findViewById(R.id.right).setOnDragListener(新MyDragListener());    }    私有类MyTouchListener实现View.OnTouchListener {
        公共布尔onTouch(查看视图,MotionEvent motionEvent){
            如果(motionEvent.getAction()== MotionEvent.ACTION_DOWN){
                ClipData数据= ClipData.newPlainText(,);
                View.DragShadowBuilder shadowBuilder =新View.DragShadowBuilder(视图);
                view.startDrag(数据,shadowBuilder,查看,0);
                //view.setVisibility(View.INVISIBLE);
                返回true;
            }其他
                返回false;
        }
    }    私有类MyLongTouchListener实现View.OnLongClickListener {
        公共布尔onLongClick(查看视图){
            ClipData数据= ClipData.newPlainText(,);
            View.DragShadowBuilder shadowBuilder =新View.DragShadowBuilder(视图);
            view.startDrag(数据,shadowBuilder,查看,0);
            //view.setVisibility(View.INVISIBLE);
            返回true;
        }
    }    类MyDragListener实现View.OnDragListener {        @TargetApi(Build.VERSION_ codeS.JELLY_BEAN)
        @覆盖
        公共ondrag当布尔(视图V,事件的dragEvent){
            INT行动= event.getAction();            开关(event.getAction()){
                案例DragEvent.ACTION_DRAG_STARTED:                    Log.i(信息,START);
                    返回true;
                案例DragEvent.ACTION_DRAG_ENTERED:                    v.setBackgroundColor(getResources()的getColor(R.color.YellowGreen));
                    Log.i(信息,ENTER);
                    返回true;
                案例DragEvent.ACTION_DRAG_EXITED:                    如果(V == findViewById(R.id.left))
                        v.setBackgroundColor(getResources()的getColor(R.color.OldLace));
                    其他
                        如果(V == findViewById(R.id.right))
                            v.setBackgroundColor(getResources()的getColor(R.color.BurlyWood));                    Log.i(信息,EXIT);                    返回true;
                案例DragEvent.ACTION_DROP:
                    //丢弃,重新分配视图的ViewGroup
                    Log.i(信息,DROP);
                    查看查看=(查看)event.getLocalState();
                    业主的ViewGroup =(ViewGroup中)view.getParent();                    如果(owner.getId()!= R.id.top){
                        //从布局删除贴纸
                        owner.removeView(视图);                        //添加贴纸新布局
                        AbsoluteLayout容器=(AbsoluteLayout)V;
                        AbsoluteLayout.LayoutParams PARAMS =新AbsoluteLayout.LayoutParams(200,200,(int)的event.getX() - 100,(int)的event.getY() - 100);
                        container.addView(视图,则params);
                        view.setVisibility(View.VISIBLE);
                        view.setFocusable(假);                        //设置onLongClickListener
                        view.setOnLongClickListener(新MyLongTouchListener());                        Log.i(信息,移动操作);                    }
                    否则如果(owner.getId()== R.id.top){                        //view.setVisibility(View.VISIBLE);                        //由code新的EditText对象
                        EDITTEXT的EditText =新的EditText(Main.this);
                        editText.setBackgroundDrawable(getResources()getDrawable(R.drawable.note));                        //添加贴纸集装箱
                        AbsoluteLayout容器=(AbsoluteLayout)V;
                        AbsoluteLayout.LayoutParams PARAMS =新AbsoluteLayout.LayoutParams(200,200,(int)的event.getX() - 100,(int)的event.getY() - 100);
                        container.addView(EDITTEXT,则params);                        //将焦点设置的EditText
                        editText.setFocusable(假);                        //设置onLongClickListener
                        editText.setOnLongClickListener(新MyLongTouchListener());                        Log.i(信息,复制操作);                    }                    //设置视图父母正常
                    如果(ⅴ== findViewById(R.id.left)){
                        v.setBackgroundColor(getResources()的getColor(R.color.OldLace));
                    }
                    其他
                    如果(ⅴ== findViewById(R.id.right)){
                        v.setBackgroundColor(getResources()的getColor(R.color.BurlyWood));
                    }                    返回true;                案例DragEvent.ACTION_DRAG_ENDED:
                    Log.i(信息,拖放结果:+ event.getResult());                    如果(event.getResult()==假){                        吐司面包= Toast.makeText(Main.this,无效的操作拖放,Toast.LENGTH_SHORT);
                        toast.show();
                        //下降时不采取行动处理程序
                        ((查看)event.getLocalState())setVisibility(View.VISIBLE);
                    }                    Log.i(信息,END *******************);                    返回true;                默认:
                    打破;
            }
            返回false;
        }
    }
}


解决方案

与移动的EditText,你应该做它的一个副本,在视图中。例如,你应该做到以下几点:

 字符串editTextString = editText.getText()的toString()。
的EditText newEditText =新的EditText(MainActivity.this);
newEditText.setText(editTextString);

所以,你需要复制从的EditText文本并创建一个新的EditText您将添加到容器中。请确保您不会使视图不可见时调用v.startDrag(数据,shadowBuilder,V,0);

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;
        }
    }


}

解决方案

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);

这篇关于通过拖放Android的问题创建的EditText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆