java - 用于自定义 View 的 Android onTouchEvent

标签 java android

我创建了一个在自定义 LinearLayout 中显示自定义 View 的服务。该服务工作正常,View 显示在屏幕上,但我无法让 ViewLinearLayout 接收任何触摸事件(我只希望其中之一接收触摸事件,我不在乎哪个)。这是我的:

MyLayout.java

public class MyLayout extends LinearLayout {

    public MyLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public MyLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyLayout(Context context) {
        super(context);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        onTouchEvent(ev);
        return true;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Toast.makeText(this.getContext(), "Touched layout", Toast.LENGTH_SHORT).show();
        Log.d("TOUCH", "Touched layout");
        return true;
    }
}

我的 View .java

public class MyViewextends View{

    public MyView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyView(Context context) {
        super(context);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Toast.makeText(this.getContext(), "Touched View", Toast.LENGTH_SHORT).show();
        Log.d("TOUCH", "Touched View");
        return true;
    }
}

最佳答案

来自 SDK 文档:

onTouch() - This returns a boolean to indicate whether your listener consumes this event. The important thing is that this event can have multiple actions that follow each other. So, if you return false when the down action event is received, you indicate that you have not consumed the event and are also not interested in subsequent actions from this event. Thus, you will not be called for any other actions within the event, such as a finger gesture, or the eventual up action event.

您需要在触发 ACTION_DOWN 事件时返回 true,以表明您对与同一事件相关的后续调用感兴趣。

如果您需要在 View 类中编写 Android onTouchEvent 方法,这里有一些示例源代码(样板/骨架代码)展示了如何实现此方法,包括如何在方法中使用 MotionEvent,以及如何获取触摸事件的 x 和 y 位置:

public boolean onTouchEvent(MotionEvent event) {

    int eventAction = event.getAction();

    // you may need the x/y location
    int x = (int)event.getX();
    int y = (int)event.getY();

    // put your code in here to handle the event
    switch (eventAction) {
        case MotionEvent.ACTION_DOWN:
            break;
        case MotionEvent.ACTION_UP:
            break;
        case MotionEvent.ACTION_MOVE:
            break;
    }

    // tell the View to redraw the Canvas
    invalidate();

    // tell the View that we handled the event
    return true;

}

我对代码进行了注释,我认为它展示了这种方法最常见的使用方式,所以我不会在这里添加任何解释。我展示了如何获取触摸事件的 x 和 y 位置,因为您通常想要检查事件发生的位置。

因此,总而言之,如果您想查看 Android onTouchEvent 方法示例(在 View 类中),我希望这个示例对您有所帮助。

引用:http://alvinalexander.com/android/android-ontouchevent-example-method-view-class

关于java - 用于自定义 View 的 Android onTouchEvent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34550303/

相关文章:

android - 将 android-maps-utils 与 ADT 结合使用

java - 将 jdouble* native 变量解析为 double* native 变量 (jni)

java - 元素不可见异常 - 即使使用不同的 Selenium 等待

android - 使用 retrofit 2 发送 @Multipart 文件/位图和额外参数的最佳方法是什么?

java - 将号码从 NumberPicker 发送到 Firebase

android - 以编程方式将我的应用程序设置为android中的默认音乐播放器

java - Hadoop,mapreduce java.io.IOException : Type mismatch in value from map: expected org. apache.hadoop.io.Text,收到 org.apache.hadoop.io.IntWritable

java - hibernate支持自关联吗?

java - 同步块(synchronized block)中的notify()

android - 如何解析既可以是对象也可以是数组的JSON?