安卓下降后,EDITTEXT正在被拖入在前看不见 [英] Android: After drop, editText that was being dragged disapears

查看:458
本文介绍了安卓下降后,EDITTEXT正在被拖入在前看不见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创造一个用户可以移动输入的形式。我有一个EDITTEXT,并希望它能够将它移动到新位置,在我的RelativeLayout。我可以把它捡起来,并四处移动它,但是一旦我放手,它完全消失。我试过pretty太多的一切。我认为方法ondrag当可能不会永远被调用,因为我从来没有记录显示。还有,我EDITTEXT的后放手,我在logcat中收到此错误信息:报告结果下降:假

我一直有另一个问题是,当你轻点EDITTEXT,它认为我试图拖动它意味着我无法真正改变它里面的文字。

任何帮助AP preciated!谢谢!

下面是我的相对布局:

 <?XML版本=1.0编码=UTF-8&GT?;
<的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
                的xmlns:程序=htt​​p://schemas.android.com/apk/res-auto
                的xmlns:工具=htt​​p://schemas.android.com/tool​​s
                机器人:layout_width =match_parent
                机器人:layout_height =match_parent
                机器人:paddingBottom会=@扪/ activity_vertical_margin
                机器人:paddingLeft =@扪/ activity_horizo​​ntal_margin
                机器人:paddingRight =@扪/ activity_horizo​​ntal_margin
                机器人:paddingTop =@扪/ activity_vertical_margin
                应用:layout_behavior =@字符串/ appbar_scrolling_view_behavior
                工具:上下文=com.supercilex.myapp.NewForm
                工具:舒=@布局/ activity_new_form
    机器人:ID =@ + ID / RelativeLayout的>    <的EditText
        机器人:ID =@ + ID / EDITTEXT
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentStart =真
        机器人:layout_alignParentTop =真
        机器人:EMS =10
        机器人:提示=输入一些文字
        安卓的inputType =textPersonName/>
< / RelativeLayout的>

下面是我的类:

 进口android.content.ClipData;
进口android.content.ClipDesc​​ription;
进口android.os.Bundle;
进口android.support.design.widget.FloatingActionButton;
进口android.support.v7.app.AppCompatActivity;
进口android.support.v7.widget.Toolbar;
进口android.util.Log;
进口android.view.DragEvent;
进口android.view.MotionEvent;
进口android.view.View;
进口android.widget.EditText;
进口android.widget.RelativeLayout;进口common.FileManagement;公共类NewForm扩展AppCompatActivity
{
    私有静态的EditText EDITTEXT;
    私人android.widget.RelativeLayout.LayoutParams的LayoutParams;
    弦乐味精;    @覆盖
    保护无效的onCreate(捆绑savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_new_form);
        工具条工具栏=(栏)findViewById(R.id.toolbar);
        setSupportActionBar(工具栏);
        EDITTEXT =(EditText上)findViewById(R.id.editText);
        文件管理文件管理=新的文件管理();        editText.setText(fileManagement.read(textViewData,getApplicationContext()));        FloatingActionButton FAB =(FloatingActionButton)findViewById(R.id.fab);
        fab.setOnClickListener(新View.OnClickListener()
        {
            @覆盖
            公共无效的onClick(查看视图)
            {
                文件管理文件管理=新的文件管理();
                串tmpEditText = editText.getText()的ToString();
                MainActivity.textView.setText(tmpEditText);
                fileManagement.save(textViewData,tmpEditText,getApplicationContext());
                完成();
            }
        });        editText.setOnLongClickListener(新View.OnLongClickListener(){
            @覆盖
            公共布尔onLongClick(视图v){
                ClipData.Item项目=新ClipData.Item((CharSequence的)v.getTag());
                的String [] = MIMETYPES {ClipDesc​​ription.MIMETYPE_TEXT_PLAIN};                ClipData dragData =新ClipData(v.getTag()的toString(),MIME类型,项目);
                View.DragShadowBuilder myShadow =新View.DragShadowBuilder(EDITTEXT);                v.startDrag(dragData,myShadow,NULL,0);
                返回true;
            }
        });        editText.setOnDragListener(新View.OnDragListener(){
            @覆盖
            公共ondrag当布尔(视图V,事件的dragEvent){
                开关(event.getAction())
                {
                    案例DragEvent.ACTION_DRAG_STARTED:
                        的LayoutParams =(RelativeLayout.LayoutParams)v.getLayoutParams();
                        Log.d(味精,行动是DragEvent.ACTION_DRAG_STARTED);
                        返回true;                    案例DragEvent.ACTION_DRAG_ENTERED:
                        Log.d(味精,行动是DragEvent.ACTION_DRAG_ENTERED);
                        INT x_cord =(int)的event.getX();
                        INT y_cord =(int)的event.getY();
                        返回true;                    案例DragEvent.ACTION_DRAG_EXITED:
                        Log.d(味精,行动是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);
                        返回true;                    案例DragEvent.ACTION_DRAG_LOCATION:
                        Log.d(味精,行动是DragEvent.ACTION_DRAG_LOCATION);
                        x_cord =(int)的event.getX();
                        y_cord =(int)的event.getY();
                        返回true;                    案例DragEvent.ACTION_DRAG_ENDED:
                        Log.d(味精,行动是DragEvent.ACTION_DRAG_ENDED);                        返回true;                    案例DragEvent.ACTION_DROP:
                        Log.d(味精,ACTION_DROP事件);
                        返回true;
                    默认:
                        打破;
                }
                返回true;
            }
        });        editText.setOnTouchListener(新View.OnTouchListener(){
            @覆盖
            公共布尔onTouch(视图V,MotionEvent事件){
                如果(event.getAction()== MotionEvent.ACTION_DOWN){
                    ClipData数据= ClipData.newPlainText(,);
                    View.DragShadowBuilder shadowBuilder =新View.DragShadowBuilder(EDITTEXT);                    editText.startDrag(数据,shadowBuilder,EDITTEXT,0);
                    editText.setVisibility(View.INVISIBLE);
                    返回true;
                }
                其他
                {
                    返回false;
                }
            }
        });
    }
}

我也试过我与方法ondrag当突破; 而不是返回true;
我尝试了这种方式:

 公共ondrag当布尔(视图V,事件的dragEvent){
                开关(event.getAction())
                {
                    案例DragEvent.ACTION_DRAG_STARTED:
                        打破;                    案例DragEvent.ACTION_DRAG_ENTERED:
                        打破;                    案例DragEvent.ACTION_DRAG_EXITED:
                        打破;                    案例DragEvent.ACTION_DRAG_LOCATION:
                        打破;                    案例DragEvent.ACTION_DRAG_ENDED:
                        打破;                    案例DragEvent.ACTION_DROP:
                        查看查看=(查看)event.getLocalState();
                        业主的ViewGroup =(ViewGroup中)view.getParent();
                        owner.removeView(视图);
                        容器的FrameLayout =(的FrameLayout)V;
                        container.addView(视图);
                        view.setVisibility(View.VISIBLE);
                        Log.d(味精,ACTION_DROP事件);
                        打破;
                    默认:
                        打破;
                }
                返回true;
            }

编辑:
这是我更新code:

以上的onCreate()

 私人的EditText EDITTEXT;
私人RelativeLayout.LayoutParams的LayoutParams;

剩下的:

  findViewById(R.id.yourlayoutname).setOnDragListener(新View.OnDragListener()
            {
                @覆盖
                公共ondrag当布尔(视图V,事件的dragEvent)
                {
                    如果(event.getAction()== DragEvent.ACTION_DROP)
                    {
                        INT xCord =(int)的event.getX();
                        INT yCord =(int)的event.getY();
                        INT高度= editText.getHeight();
                        INT宽度= editText.getWidth();
                        layoutParams.leftMargin = xCord - 宽度;
                        layoutParams.topMargin = yCord - 高度;
                        editText.setLayoutParams(的LayoutParams);
                    }
                    返回true;
                }
            });            editText.setOnLongClickListener(新View.OnLongClickListener(){
                @覆盖
                公共布尔onLongClick(视图v){
                    的String [] = MIMETYPES {ClipDesc​​ription.MIMETYPE_TEXT_PLAIN};                    ClipData数据= ClipData.newPlainText(,);
                    View.DragShadowBuilder shadowBuilder =新View.DragShadowBuilder(EDITTEXT);                    v.startDrag(数据,shadowBuilder,MIMETYPES,0);
                    返回true;
                }
            });            editText.setOnDragListener(新View.OnDragListener()
            {
                @覆盖
                公共ondrag当布尔(视图V,事件的dragEvent)
                {
                    如果(event.getAction()== DragEvent.ACTION_DRAG_STARTED)
                    {
                        的LayoutParams =(RelativeLayout.LayoutParams)v.getLayoutParams();
                    }
                    返回true;
                }
            });


解决方案

我在logcat中收到此错误信息:报告结果下降:假
这不是错误消息。这意味着该行动降不返回真正由结果查看 DragListener

的EditText 消失了,因为你将它设置为invisiblity View.INVISIBLE 里面的 onTouchListener 并没有改变它回来了。

试试这个:
如果你想通过触摸或长按拖动先选择。你不需要两者。比方说,你决定通过触摸来做到这一点:

  editText.setOnTouchListener(新View.OnTouchListener(){
        @覆盖
        公共布尔onTouch(视图V,MotionEvent事件){
            如果(event.getAction()== MotionEvent.ACTION_DOWN){
                ClipData数据= ClipData.newPlainText(,);
                View.DragShadowBuilder shadowBuilder =新View.DragShadowBuilder(EDITTEXT);                editText.startDrag(数据,shadowBuilder,EDITTEXT,0);
                editText.setVisibility(View.INVISIBLE);
                返回true;
            }
            其他
            {
                返回false;
            }
        }
    });

从code监听器删除所有reduntant code,它无需拖动作为系统为您处理的拖累。如果您想要投递的配位是的EditText 不是你需要指定 DragListener 到新位置的EditText 容器 RelativeLayout的和`ACTION_DROP检索。如果不是:

  editText.setOnDragListener(新View.OnDragListener(){
        @覆盖
        公共ondrag当布尔(视图V,事件的dragEvent){
            如果(event.getAction == DragEvent.ACTION_DRAG_STARTED){
                    的LayoutParams =(RelativeLayout.LayoutParams)v.getLayoutParams();
            }否则如果(event.getAction()== DragEvent.ACTION_DRAG_ENDED)
                v.setVisibility(View.VISIBLE)            返回true;
        }
    });

有关获取放置位置你需要这样的事情:

  findViewById(R.id.relativeLayout).setOnDragListener(新View.OnDragListener(){
    @覆盖
    公共ondrag当布尔(视图V,事件的dragEvent){
        如果(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);        }
        返回true;
    }
});

I'm trying to create a form in which the user can move inputs around. I have an editText and want it to be able to move it to a new location in my relativeLayout. I am able to pick it up and move it around, but once I let go, it disappears completely. I've tried pretty much everything. I think the onDrag method may not ever be called because my logs never show up. Also, after a let go of my editText, I get this error message in logcat: Reporting drop result: false

Another issue I've been having is that when you tap the editText, it think I'm trying to drag it meaning I can't actually change the text inside it.

Any help is appreciated! Thanks!

Here is my relative layout:

<?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>

Here is my class:

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

I've also tried my onDrag method with break; instead of return true; And I've tried it this way:

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

Edit: Here is my updated code:

Above onCreate()

private EditText editText;
private RelativeLayout.LayoutParams layoutParams; 

The rest:

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

解决方案

I get this error message in logcat: Reporting drop result: false It's not error message. It's mean that the Action drop not returned true as result by the View DragListener.

Your EditText disappear because you set it invisiblity to View.INVISIBLE inside your onTouchListener and never changed it back.

try this: First choose if you want to Drag by Touch or by Long Click. You don't need both. Let's say you decided to do it by touch:

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

Remove all the reduntant code from the code Listener, no need it to the Drag as the system handle the drag for you. If you want the coordinations of the Drop to be the new location of the EditText than you need to assign DragListener to the EditText container RelativeLayout and retrieve them in `ACTION_DROP. If not:

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

For getting the drop location you need something like that:

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

这篇关于安卓下降后,EDITTEXT正在被拖入在前看不见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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