检查触摸相对布局

时间:2015-11-01 14:37:44

标签: android

我想检查x,y position中的触摸activity是否知道它是否来自我的RelativeLayout。 我覆盖活动的onTouch事件,但它无法正常工作。

1 个答案:

答案 0 :(得分:4)

你必须做这样的事情..在Activity你必须覆盖

dispatchTouchEvent

所以在onCreate找到你的观点

RelativeLayout  yourLayout = (RelativeLayout) findViewById(R.id.yourLayout);

然后

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {

        float touchPointX = ev.getX();
        float touchPointY = ev.getY();
        int[] coordinates = new int[2];
        yourLayout.getLocationOnScreen(coordinates);
        if (touchPointX < coordinates[0] || touchPointX > coordinates[0] + yourLayout.getWidth() || touchPointY < coordinates[1] || touchPointY > coordinates[1] + yourLayout.getHeight()){
            //Do Your ACTION
   }
    return super.dispatchTouchEvent(ev);
    }