拖动后如何使EditText可编辑

时间:2013-11-12 03:57:07

标签: android

我正在创建一个可拖动的editText,我使用editText来允许用户更改文本。但我现在的问题是我不能再编辑文本,因为它检测到文本的触摸/拖动。

对此有何解决方案?

private final class MyTouchListener implements OnTouchListener {
    public boolean onTouch(View view, MotionEvent motionEvent) {

        switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) 
        {
            case MotionEvent.ACTION_DOWN:
             ClipData data = ClipData.newPlainText("", "");
              DragShadowBuilder shadowBuilder = new DragShadowBuilder(view);
              view.startDrag(data, shadowBuilder, view, 0);
              view.setVisibility(View.VISIBLE);
             break;
            case MotionEvent.ACTION_MOVE:
                 break;
            case MotionEvent.ACTION_UP:
                break;

            case MotionEvent.ACTION_HOVER_EXIT:
                    break;
        }
    return true;
    }

}

我有拖拽监听器

  class MyDragListener implements OnDragListener {
    @Override
      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_DROP:
        View view = (View) event.getLocalState();
         paramsBlock = new RelativeLayout.LayoutParams(view.getWidth(), view.getWidth());  
          paramsBlock.leftMargin = (int) event.getX() - (view.getWidth()/2);//- ((imageView.getWidth()/9)/2);
          paramsBlock.topMargin = (int) event.getY() -(view.getHeight()/2);//-((imageView.getWidth()/9)/2);

          view.setLayoutParams(paramsBlock);             
          break;
        case DragEvent.ACTION_DRAG_ENDED:
          default:
          break;
        }
        return true;
      }
  }

是否可以使用onLongClick听众?

1 个答案:

答案 0 :(得分:2)

您可以使用onLongClickListener开始拖动。这应该允许您编辑该字段,因为控件的默认点击或触摸监听器不会被覆盖

尝试使用类似的内容进行拖动

OnLongClickListener longClickListener = new OnLongClickListener() {

    @Override
    public boolean onLongClick(View view) {
        ClipData data = ClipData.newPlainText("", "");
        DragShadowBuilder shadowBuilder = new DragShadowBuilder(view);
        view.startDrag(data, shadowBuilder, view, 0);
        view.setVisibility(View.VISIBLE);

        return true;
    }
};

希望这有帮助