how to get a view from an event coordinates in android?

前端 未结 3 1798
鱼传尺愫
鱼传尺愫 2020-12-01 03:36

I want to intercept the touch events on my parent view with onInterceptTouchEvent (MotionEvent ev).

From there I want to know which view was clicked in

3条回答
  •  Happy的楠姐
    2020-12-01 04:18

    A simple way for getting the touched view is to set an OnTouchListener to the individual views and store the view in a class variable of the activity. Returning false will make the input event available to the method onTouchEvent() of the activity, where you can easily handle all the touch events (also the ones of your parent view).

    myView.setOnTouchListener(new OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
        touchedView = myView;
        return false;
        }
    }); 
    
    
    @Override
    public boolean onTouchEvent(MotionEvent event) {
    
    
        switch (event.getAction()) {
    
            case MotionEvent.ACTION_UP:
    
                if(touchedView!=null) {
                    doStuffWithMyView(touchedView);
                ....
                ....
    

提交回复
热议问题