Reputation: 19
i have a project that is supporting multi touch. When you touches a button it passes 1. However, it only gets the first touch . pls help me, it is for our thesis.
here is my code:
public boolean onTouch(View v, MotionEvent event) {
int action = event.getActionMasked();
if(action == MotionEvent.ACTION_DOWN){
b1=b2=b3=b4=b5=b6="0";
switch(v.getId()) {
case R.id.button3:
b3 = "1";
break;
case R.id.button2:
b2 = "1";
break;
case R.id.button1:
b1 = "1";
break;
case R.id.button4:
b4 = "1";
break;
case R.id.button5:
b5 = "1";
break;
case R.id.button6:
b6 = "1";
}
} else if(action == MotionEvent.ACTION_POINTER_DOWN )
{
switch(v.getId()) {
case R.id.button3:
b3 = "1";
break;
case R.id.button2:
b2 = "1";
break;
case R.id.button1:
b1 = "1";
break;
case R.id.button4:
b4 = "1";
break;
case R.id.button5:
b5 = "1";
break;
case R.id.button6:
b6 = "1";
}
}
}
in here when the button1 and button2 is pressed it should pass b1=1 and b2=1 however it only passes the b1=1.
Upvotes: 1
Views: 318
Reputation: 63293
The problem with your touch listener (assuming that all buttons are calling into the same method) is that it resets the values each time you come into the method, and onTouch()
will get called once for each event you need to process. In your example where you touch two unique buttons passing through the same listener, the method will be called once with an ACTION_DOWN
event for the first button you touch, and then a second time with an ACTION_DOWN
event for the second button you touch. Your second run through the method resets the values of all bx
inputs back to zero before setting the value associated with that view, so this code will always only leave one button "marked" as touched.
You might consider resetting the "mark" state on the ACTION_UP
and ACTION_CANCEL
events for that view instead to keep a better global state. I would also agree with other posters that storing these values as strings seems quite inefficient and will make comparison logic more difficult later on.
Upvotes: 1
Reputation: 179392
Return true
to indicate you handled the touch event.
Also, using strings (b1
through b6
) as booleans is a patently bad idea. Use a boolean
instead.
Upvotes: 2