Android 如何使用 View.OnTouchListener 而不是 onClick

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11690504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:28:44  来源:igfitidea点击:

How to use View.OnTouchListener instead of onClick

androidandroid-layoutuser-input

提问by VansFannel

I'm developing an Android 2.2.2 application for a client and he wants to do the following:

我正在为客户开发一个 Android 2.2.2 应用程序,他想执行以下操作:

Now I have a button with an onClick event but he doesn't like, he wants to dectect when user release the button.

现在我有一个带有 onClick 事件的按钮,但他不喜欢,他想检测用户何时释放按钮。

I've found View.OnTouchListenerwhich I think this is what I need to use but, is there any posibility to add this event to xml like I did with onClick?

我找到了View.OnTouchListener,我认为这是我需要使用的,但是,是否有可能像使用 onClick 那样将此事件添加到 xml 中?

<ImageButton
    android:id="@+id/btnSaveNewGate"
    android:layout_width="@dimen/btnSaveNewGate_width"
    android:layout_height="@dimen/btnSaveNewGate_height"
    android:layout_below="@+id/radioGrGateType"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="@dimen/btnSaveNewGate_marginTop"
    android:background="@null"
    android:contentDescription="@string/layout_empty"
    android:onClick="onSaveNewGateClick"
    android:scaleType="fitXY"
    android:src="@drawable/save_gate_selector" />

I have two questions more:

我还有两个问题:

Which is the event associated when user releases his finger?

用户松开手指时关联的事件是什么?

Is there any guidelines which prohibit using View.OnTouchListenerinstead of onClick?

是否有任何准则禁止使用View.OnTouchListener代替onClick

回答by Andy Res

The event when user releases his finger is MotionEvent.ACTION_UP. I'm not aware if there are any guidelines which prohibit using View.OnTouchListener instead of onClick(), most probably it depends of situation.

用户松开手指的事件是MotionEvent.ACTION_UP。我不知道是否有任何准则禁止使用 View.OnTouchListener 而不是 onClick(),这很可能取决于具体情况。

Here's a sample code:

这是一个示例代码:

imageButton.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if(event.getAction() == MotionEvent.ACTION_UP){

            // Do what you want
            return true;
        }
        return false;
    }
});

回答by Suragch

Presumably, if one wants to use an OnTouchListenerrather than an OnClickListener, then the extra functionality of the OnTouchListeneris needed. This is a supplemental answer to show more detail of how an OnTouchListenercan be used.

据推测,如果想要使用 anOnTouchListener而不是 an OnClickListener,则需要 的额外功能OnTouchListener。这是一个补充答案,以显示有关如何OnTouchListener使用a 的更多详细信息。

Define the listener

定义监听器

Put this somewhere in your activity or fragment.

把它放在你的活动或片段中的某个地方。

private View.OnTouchListener handleTouch = new View.OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {

        int x = (int) event.getX();
        int y = (int) event.getY();

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                Log.i("TAG", "touched down");
                break;
            case MotionEvent.ACTION_MOVE:
                Log.i("TAG", "moving: (" + x + ", " + y + ")");
                break;
            case MotionEvent.ACTION_UP:
                Log.i("TAG", "touched up");
                break;
        }

        return true;
    }
};

Set the listener

设置监听器

Set the listener in onCreate(for an Activity) or onCreateView(for a Fragment).

将侦听器设置为onCreate(对于活动)或onCreateView(对于片段)。

myView.setOnTouchListener(handleTouch);

Notes

笔记

  • getXand getYgive you the coordinates relative to the view (that is, the top left corner of the view). They will be negative when moving above or to the left of your view. Use getRawXand getRawYif you want the absolute screen coordinates.
  • You can use the xand yvalues to determine things like swipe direction.
  • getXgetY为您提供相对于视图的坐标(即视图的左上角)。在您的视图上方或左侧移动时,它们将为负值。如果您想要绝对屏幕坐标,请使用getRawXgetRawY
  • 您可以使用xy值来确定诸如滑动方向之类​​的事情。

回答by CaseyB

OnClick is triggered when the user releases the button. But if you still want to use the TouchListener you need to add it in code. It's just:

当用户释放按钮时触发 OnClick。但是,如果您仍想使用 TouchListener,则需要在代码中添加它。只是:

myView.setOnTouchListener(new View.OnTouchListener()
{
    // Implementation;
});

回答by RahimpoorDeveloper

for use sample touch listener just you need this code

要使用示例触摸侦听器,您只需要此代码

@Override
public boolean onTouch(View view, MotionEvent motionEvent) {

    ClipData data = ClipData.newPlainText("", "");
    View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
    view.startDrag(data, shadowBuilder, null, 0);

    return true;
}