这是我用于正向和反向的代码
// Drive forward
forward_button.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if ((event.getAction() == MotionEvent.ACTION_DOWN) | (event.getAction() == MotionEvent.ACTION_MOVE)) {
forward_button.setPressed(true);
Arduino = (byte) (Arduino | 16);
write(Arduino);
return true;
}else if (event.getAction() == MotionEvent.ACTION_UP) {
forward_button.setPressed(false);
Arduino = (byte) (Arduino & 236);
write(Arduino);
return true;
}
forward_button.setPressed(false);
return false;
}
});
// Back up
reverse_button.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if ((event.getAction() == MotionEvent.ACTION_DOWN) | (event.getAction() == MotionEvent.ACTION_MOVE)) {
reverse_button.setPressed(true);
Arduino = (byte) (Arduino | 32);
write(Arduino);
return true;
}else if (event.getAction() == MotionEvent.ACTION_UP) {
reverse_button.setPressed(false);
Arduino = (byte) (Arduino & 220);
write(Arduino);
return true;
}
reverse_button.setPressed(false);
return false;
}
});
我正在考虑对左右功能使用相同的方式,例如;
left_button.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if ((event.getAction() == MotionEvent.ACTION_DOWN) | (event.getAction() == MotionEvent.ACTION_MOVE)) {
left_button.setPressed(true);
Arduino = (byte) (Arduino | 8);
write(Arduino);
return true;
}else if (event.getAction() == MotionEvent.ACTION_UP) {
left_button.setPressed(false);
Arduino = (byte) (Arduino & 244);
write(Arduino);
return true;
}
left_button.setPressed(false);
return false;
}
});
这是一种方法吗??