问题
So I have two buttons and would like to press both at the same time. If I press the first it logs a "C" (as a piano note), the other logs a D.
So far:
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getActionMasked();
switch (event.getAction() & MotionEvent.ACTION_MASK){
case MotionEvent.ACTION_DOWN:
switch (v.getId()) {
case R.id.cnotebutton:
Log.i("C", "C1");
return true;
case R.id.c2notebutton:
Log.i("D", "D1");
return true;
default:
}
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_POINTER_DOWN:
break;
case MotionEvent.ACTION_POINTER_UP:
switch (v.getId()) {
case R.id.cnotebutton:
Log.i("C", "C2");
return true;
case R.id.c2notebutton:
Log.i("D", "D2");
return true;
default:
}
break;
case MotionEvent.ACTION_MOVE:
break;
default:
}
return true;
}
I Log C1 and C2 to distinguish the cases, but every time I press the buttons I get only C1,C2 or D1,D2 as if I touched the same buttons with both of my fingers. I should get C1,D2 or D1,C2 depending on which finger pointed first. Any suggestions? Anyway I havent found a piano sample project yet, but that would definetly help me, if this is not going to work or my approach is totally wrong. Is there any?
Thanks in advance!
回答1:
I think public boolean onInterceptTouchEvent (MotionEvent ev)
is what you are looking for. Take a look at this.
来源:https://stackoverflow.com/questions/13722595/multitouch-issue-when-pressing-two-buttons-at-the-same-time-it-detects-as-if-i