prevent阻力定制MIMETYPE跌落到的EditText [英] Prevent drag drop of custom mimetype to EditText

查看:233
本文介绍了prevent阻力定制MIMETYPE跌落到的EditText的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我打算使用的应用程序中拖放应用程序对象的自定义MIME类型。这似乎是工作,但我发现了的EditText字段也接受拖放操作。我不希望这种事情发生。

I have a custom mime type which I am intending to use to drag and drop application objects within the app. This seems to be working but I'm finding that the EditText fields are also accepting the drop action. I don't want this to happen.

首先,我定义的custome MIME类型是这样的:

First, I've defined the custome mime type like this:

public static final String MIME_TYPE_MYNODE = "com.example.mockup/mynode";

然后,在onTouch处理程序为源对象我有:

Then, in the onTouch handler for the source object I have:

  @Override
  //-----------------------------------------------------------------------------
  public boolean onTouch (View v, MotionEvent e)
  {
    ...
    else if (e.getAction() == MotionEvent.ACTION_MOVE)
    {
      String[] mimeTypes = {MIME_TYPE_MYNODE};
      ClipData data = new ClipData ("Task Tamer Note", mimeTypes, new ClipData.Item ("unused"));
      View.DragShadowBuilder shadow = new View.DragShadowBuilder(this);
      Object localState = v;
      startDrag (data, shadow, localState, 0);
      return false;
    }
  }
  ...
}

当我在一个EditText小部件滴,它插入未使用到文本区域。我怎样才能prevent呢? 谢谢你。

When I "drop" on an EditText widget, it inserts "unused" into the text area. How can I prevent this? Thanks.

推荐答案

我遇到相同的行为。我已经找到了原因,它位于TextView的类。

I encountered the same behaviour. I have found the reason, which is located in TextView class.

该方法onDragEvent(的dragEvent事件)在​​这里overrriden,看起来如下。

The method onDragEvent(DragEvent event) is overrriden here and looks as below.

@Override
public boolean onDragEvent(DragEvent event) {
    switch (event.getAction()) {
        case DragEvent.ACTION_DRAG_STARTED:
            return mEditor != null && mEditor.hasInsertionController();

        case DragEvent.ACTION_DRAG_ENTERED:
            TextView.this.requestFocus();
            return true;

        case DragEvent.ACTION_DRAG_LOCATION:
            final int offset = getOffsetForPosition(event.getX(), event.getY());
            Selection.setSelection((Spannable)mText, offset);
            return true;

        case DragEvent.ACTION_DROP:
            if (mEditor != null) mEditor.onDrop(event);
            return true;

        case DragEvent.ACTION_DRAG_ENDED:
        case DragEvent.ACTION_DRAG_EXITED:
        default:
            return true;
    }
}

如果能够插入文本=>的EditText然后任何阻力将被处理,并接受在那里。 View类不使用OnDragListener,所以它不可能prevent这种行为通过

If it is possible to insert text => EditText then any drag will be processed and accepted there. The View class is not using OnDragListener, so it is not possible to prevent this behaviour by

editText.setOnDragListener(null);

这里的解决方案是继承的EditText这里并覆盖onDragEvent()方法:

The solution here is to subclass the EditText as here and override onDragEvent() method:

 public class YourEditText extends EditText {

 ...
 // other stuff
 ...

 @Override
 public boolean onDragEvent(DragEvent event) {
    switch (event.getAction()) {
       case DragEvent.ACTION_DRAG_STARTED:
           if (event.getClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) {
               return true;
           }
           return false;
       default:
           return super.onDragEvent(event);
    }
}

}

现在YourEditText将只接受一拖再拖与MIMETYPE_TEXT_PLAIN。如果您希望禁用拖放在所有刚刚返回false在这个method`

Now YourEditText will accept only drags with MIMETYPE_TEXT_PLAIN. If you want to disable the drag drop at all just return false in this method`

@Override
public boolean onDragEvent(DragEvent event) {
    return false;    
}

就是这样。 希望这将有助于。

And that's it. Hope it will help.

这篇关于prevent阻力定制MIMETYPE跌落到的EditText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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