如何在两个或多个不同的自定义视图上应用相同的双击触控侦听器?
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);
任何人都可以帮助我吗?
答案 0 :(得分: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
}
}
答案 1 :(得分: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;
}
});
它只为我工作......