7

在我的应用程序中,我需要在同一个 textView 上同时使用单击和拖动事件。

我写了以下代码:

... }
    switch(event.getAction()) {
        case MotionEvent.ACTION_UP:
//            TextDialog.setVisibility(View.VISIBLE);
            break;
        case MotionEvent.ACTION_DOWN: {
            disallowTouch(parent, true);
            int downX = (int)event.getX();
            int downY = (int)event.getY();
            return false; // allow other events like Click to be processed
        }
        case MotionEvent.ACTION_MOVE:
            int x = (int)event.getRawX();
            int y= (int)event.getRawY();

            layoutParams.leftMargin = x - 50;
            layoutParams.topMargin = y - 70;
                                       
            tvText.setLayoutParams(layoutParams);
            break;
        default:
            break;
    }
    return true;
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    TextDialog.setVisibility(View.VISIBLE);
}

但只有ACTION_MOVE工作。该onClick事件没有被触发。

我只想在单击 TextView 时显示一个对话框。

我怎样才能做到这一点?

4

2 回答 2

2

同时使用 setOnTouchListener 和 setOnClickListener

在 onTouch 中:

public boolean onTouch(View v, MotionEvent event) {
    ViewParent parent = v.getParent();
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN: {
        disallowTouch(parent, true);
        downX = event.getX();
        downY = event.getY();
        return false; // allow other events like Click to be processed
    }
}

private void disallowTouch(ViewParent parent, boolean isDisallow) {
    if (parent != null) {
        parent.requestDisallowInterceptTouchEvent(isDisallow);
    }
}
于 2013-02-01T07:59:46.190 回答
1

我认为您不应该使用MotionEvent.ACTION_DOWN,但只能使用MotionEvent.ACTION_UP

public boolean onTouch(View view, MotionEvent motionEvent) {
    switch (motionEvent.getAction()) {
        // OnClick
        case MotionEvent.ACTION_UP: {
            TextDialog.setVisibility(View.VISIBLE);                
            return false;  // To allow other events
        }
        // OnTouch
        case MotionEvent.ACTION_MOVE: {
            ClipData clipData = ClipData.newPlainText("", "");
            View.DragShadowBuilder dragShadowBuilder = new View.DragShadowBuilder(view);
            view.startDragAndDrop(clipData, dragShadowBuilder, null, 0);
            return true;
        }
    }
}
于 2022-01-26T14:58:03.820 回答