Android - 劫持点击

发布于 2024-10-09 07:48:39 字数 1436 浏览 3 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心的位置 2024-10-16 07:48:39

我自己刚刚找到了解决方案。它位于 onInterceptTouchEvent() 函数中。您可以覆盖它以在将所有触摸事件移交给子视图之前拦截所有触摸事件。所以我创建了一个扩展 FrameLayout 的布局。用这个布局包围我的布局,就是这样:)

i just found the solution myself. It lies in the onInterceptTouchEvent() function. You can override it to intercept all touch events before being handed over to the child views. So i created a layout extending FrameLayout. Surrounded my layout with this layout and thats it :)

画骨成沙 2024-10-16 07:48:39

您只想捕获特定视图被点击的次数吗?或者您想要触摸的精确像素坐标?如果是前者,您只需重写每个视图的 onTouchListener ,并增加该视图的计数器。

对于后者,我使用了 onTouchListener 进行了一些尝试,并且能够让它工作,但是您可能必须为每个视图设置一个 onTouchListener,这不应该是一个大问题,但只是需要保留的东西除非有人有更好的方法。

Vector2D.java

public class Vector2D {
    private float x;
    private float y;

    public Vector2D(float x, float y) {
        this.x = x;
        this.y = y;
    }

    public void setX(float x) {
        this.x = x;
    }

    public float getX() {
        return this.x;
    }

    public void setY(float y) {
        this.y = y;
    }

    public float getY() {
        return this.y;
    }
}

Main.java

public class Main extends Activity implements OnTouchListener {
    /** Called when the activity is first created. 
     * @return */

    //defined as class variable so it's accessible from onTouch()
    List<Vector2D> points;

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        //adds a new coordinate to the list, 
        //with the X and Y values of the touch
        points.add(new Vector2D(event.getX(), event.getY()));
        Log.d("TOUCH", "X:" + event.getX() + " Y:" + event.getY());
        return true;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);     

        points = new ArrayList<Vector2D>();       
        final LinearLayout ll = (LinearLayout)findViewById(R.id.linearlayout);
        final TextView pointsList = (TextView)findViewById(R.id.points_list);
        ll.setOnTouchListener(this);
        final Button listPoints = (Button)findViewById(R.id.list_points);
        listPoints.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                StringBuilder sb = new StringBuilder();
                for (Vector2D vector : points) {
                    sb.append(
                        "X:" +vector.getX() + " " + 
                        "Y:" + vector.getY() + "\n");
                }
                pointsList.setText(sb.toString());
            }
        });
    }
}

Do you just want to capture how many times a specific view is clicked? Or are you wanting the precise pixel coordinates of the touch? If the former, you could just override the onTouchListener for each of them, and increment a counter for that view.

For the latter, I played around a bit with an onTouchListener, and was able to get it to work, but you'll probably have to set an onTouchListener to every view, which shouldn't be a big deal, but just something to keep in mind unless someone has a better way.

Vector2D.java

public class Vector2D {
    private float x;
    private float y;

    public Vector2D(float x, float y) {
        this.x = x;
        this.y = y;
    }

    public void setX(float x) {
        this.x = x;
    }

    public float getX() {
        return this.x;
    }

    public void setY(float y) {
        this.y = y;
    }

    public float getY() {
        return this.y;
    }
}

Main.java

public class Main extends Activity implements OnTouchListener {
    /** Called when the activity is first created. 
     * @return */

    //defined as class variable so it's accessible from onTouch()
    List<Vector2D> points;

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        //adds a new coordinate to the list, 
        //with the X and Y values of the touch
        points.add(new Vector2D(event.getX(), event.getY()));
        Log.d("TOUCH", "X:" + event.getX() + " Y:" + event.getY());
        return true;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);     

        points = new ArrayList<Vector2D>();       
        final LinearLayout ll = (LinearLayout)findViewById(R.id.linearlayout);
        final TextView pointsList = (TextView)findViewById(R.id.points_list);
        ll.setOnTouchListener(this);
        final Button listPoints = (Button)findViewById(R.id.list_points);
        listPoints.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                StringBuilder sb = new StringBuilder();
                for (Vector2D vector : points) {
                    sb.append(
                        "X:" +vector.getX() + " " + 
                        "Y:" + vector.getY() + "\n");
                }
                pointsList.setText(sb.toString());
            }
        });
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文