我的活动中有一个OnTouchListener,它设置在布局和其他5个按钮上,如果你逐个按下它们/它们完全可以工作但是如果我想按下一个按钮仍然移动我的手指放在布局上,直到我从按钮上松开手指才能工作。 如何使用OnTouch以便可以同时使用几个视图?
@Override
public boolean onTouch(View v, MotionEvent event){
switch (v.getId()) {
case R.id.layout:
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// On click on screen
oldX = (int) event.getX(); // set first click X as old
oldY = (int) event.getY(); // set first click Y as old
isClicked = true;
return true;
}
else if (event.getAction() == MotionEvent.ACTION_MOVE) {
isClicked = false;
// On finger move on screen
newX = (int) event.getX(); // set current X
newY = (int) event.getY(); // set current Y
diff[0] = newX - oldX; // Set X difference
diff[1] = newY - oldY; // Set y difference
oldX = newX; // set current X as last X
oldY = newY; // set current Y as last Y
// Send to server
new UDP_handler(IPAddr, port).execute("MOVE|" + diff[0] + "," + diff[1]);
return true;
}
else if(event.getAction() == MotionEvent.ACTION_UP){
if (isClicked)
new UDP_handler(IPAddr, port).execute("CLICK|LC");
return true;
}
break;
case R.id.Button0:
if (event.getAction() == MotionEvent.ACTION_UP){
new UDP_handler(IPAddr, port).execute("CLICK|MUP");
return true;
}
break;
case R.id.Button1:
if (event.getAction() == MotionEvent.ACTION_UP){
new UDP_handler(IPAddr, port).execute("CLICKUP|LC");
return true;
}
else if(event.getAction() == MotionEvent.ACTION_DOWN){
new UDP_handler(IPAddr, port).execute("CLICKDOWN|LC");
return true;
}
break;
case R.id.Button2:
if (event.getAction() == MotionEvent.ACTION_UP){
new UDP_handler(IPAddr, port).execute("CLICK|MC");
return true;
}
break;
case R.id.Button3:
if (event.getAction() == MotionEvent.ACTION_UP){
new UDP_handler(IPAddr, port).execute("CLICK|RC");
return true;
}
break;
case R.id.Button4:
if (event.getAction() == MotionEvent.ACTION_UP){
new UDP_handler(IPAddr, port).execute("CLICK|MDOWN");
return true;
}
break;
default:
break;
}
return false;
}
(我想按下R.id.button1然后在R.id.layout上移动我的手指同时进行它的向下和向上动作)