1

如何在两个或多个不同的自定义视图上应用相同的双击触摸监听器?

view1.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return mGestureDetector.onTouchEvent(event);
    }
});
view2.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return mGestureDetector.onTouchEvent(event);
    }
});
mGestureDetector = new GestureDetector(new GestureDetector.SimpleOnGestureListener() {
    @Override
    public void onLongPress(MotionEvent e) 
    {
        displayPTZControlsPopup(corresponding view id); 
    }

    @Override
    public boolean onDoubleTap(MotionEvent e) 
    {
        Intent intent = new Intent(VideoView.this,FullScreenVideo.class);
        startActivity(intent);
        return true;
    }

    @Override
    public boolean onDown(MotionEvent e) 
    {
        return true;
    }
});
mGestureDetector.setIsLongpressEnabled(true);

谁能帮我?

4

2 回答 2

1

这样做:

view1.setOnTouchListener(this);
view2.setOnTouchListener(this);

@Override
public boolean onTouch(final View view, MotionEvent event) {
    if(view.getId() == idOfYourView1 || view.getId() == idOfYourView2) {
        //Apply the method to one of the view touched
    }
}
于 2012-08-09T17:18:53.093 回答
1

试试这个代码:

        GestureDetectorCompat gdc = new GestureDetectorCompat(this,new SimpleOnGestureListener(){
        @Override
        public boolean onDoubleTap(MotionEvent e) { 
        /*Your block of code*/
        /*(In my Case)*/
        startActivity(new Intent(getApplicationContext(),Your_activity.class));
            return false;
        }
    });

然后将代码分配给您的视图:

    myview = (TextView)findViewById(R.id.txtview);
    myview.setOnTouchListener(new View.OnTouchListener() {
       @Override
       public boolean onTouch(View v, MotionEvent event) {
            gdc.onTouchEvent(event);
           return true;
       }
   });

它只是为我工作......

于 2015-07-04T13:10:26.600 回答