Multiple button presses for Android 2.x
I am relatively new to this still, and I have been deve开发者_如何转开发loping a small app that would benefit greatly from a user being able to press 2 buttons at one time. What is the best method for achieving this? I dont think that an OnClickListener works like that, and I have seen examples for doing this with an OnTouch event. However, I do not know how to set up button presses with an OnTouch event.
You would have to handle touch events yourself. With the multi-pointers (aka multi-touch) API it's very easy. Just override the onTouchEvent() method or register an OnTouchListener on your buttons.
@Override
public boolean onTouchEvent (MotionEvent event) {
if (event.getAction()==MotionEvent.ACTION_UP) {
// reset all buttons
...
}
else {
int count=event.getPointerCount(),vx1=-1,vy1=-1,vx2=-1,vy2=-1;
if (count>=1) {
vx1=(int)event.getX(0);
vy1=(int)event.getY(0);
}
if (count>=2) {
vx2=(int)event.getX(1);
vy2=(int)event.getY(1);
}
...
}
return true;
}
精彩评论