onTouchListener 使用 if 语句调用方法但坚持使用一个值
onTouchListener calling method with if statement but sticks with one value
我通过 MotionEvent.ACTION_MOVE 调用此方法,但值不会随着移动发生而改变。它只保留第一次触摸时按下的第一个值。这很可能与我在这里没有退出循环的 if 语句有关,但我可以借助一些帮助
public boolean onTouch(View v, MotionEvent event) {
//_gestureDetector.onTouchEvent(event);
long timeElapsed = 0L;
int eventAction = event.getAction();
String number = "";
String letter = "";
String join = "";
int iTo = getIndexOfButton(v);
// you may need the x/y location
int x = (int)event.getX();
int y = (int)event.getY();
String position = "";
// put your code in here to handle the event
switch (eventAction) {
case MotionEvent.ACTION_DOWN:
Log.d("Chess", "Down");
timeElapsed = event.getDownTime();
break;//return true;
case MotionEvent.ACTION_UP:
Log.d("Chess", "Up");
timeElapsed = event.getDownTime() - timeElapsed;
if (timeElapsed == 2000) {
//v.performClick();
Log.d("Chess", "perform click");
}
timeElapsed = 0L;
break;//return true;
case MotionEvent.ACTION_MOVE:
respondCoordinates(v);
}
// tell the View to redraw the Canvas
//invalidate();
return false;//super.onTouchEvent(event);
}
public Boolean respondCoordinates(View v){
int iTo = getIndexOfButton(v);
String join = "";
String letter = "";
String number = "";
//boolean moveActive = true;
if ((iTo >= 0) && (iTo <= 7)) {
number = "8";
if (iTo == 0) {
letter = "a";
} else if (iTo == 1) {
letter = "b";
} else if (iTo == 2) {
letter = "c";
} else if (iTo == 3) {
letter = "d";
} else if (iTo == 4) {
letter = "e";
} else if (iTo == 5) {
letter = "f";
} else if (iTo == 6) {
letter = "g";
} else if (iTo == 7) {
letter = "h";
}
} else if ((iTo >= 8) && (iTo <= 15)) {
number = "7";
if (iTo == 8) {
letter = "a";
} else if (iTo == 9) {
letter = "b";
} else if (iTo == 10) {
letter = "c";
} else if (iTo == 11) {
letter = "d";
} else if (iTo == 12) {
letter = "e";
} else if (iTo == 13) {
letter = "f";
} else if (iTo == 14) {
letter = "g";
} else if (iTo == 15) {
letter = "h";
}
} else if ((iTo >= 16) && (iTo <= 23)) {
number = "6";
if (iTo == 16) {
letter = "a";
} else if (iTo == 17) {
letter = "b";
} else if (iTo == 18) {
letter = "c";
} else if (iTo == 19) {
letter = "d";
} else if (iTo == 20) {
letter = "e";
} else if (iTo == 21) {
letter = "f";
} else if (iTo == 22) {
letter = "g";
} else if (iTo == 23) {
letter = "h";
}
} else if ((iTo >= 24) && (iTo <= 31)) {
number = "5";
if (iTo == 24) {
letter = "a";
} else if (iTo == 25) {
letter = "b";
} else if (iTo == 26) {
letter = "c";
} else if (iTo == 27) {
letter = "d";
} else if (iTo == 28) {
letter = "e";
} else if (iTo == 29) {
letter = "f";
} else if (iTo == 30) {
letter = "g";
} else if (iTo == 31) {
letter = "h";
}
} else if ((iTo >= 32) && (iTo <= 39)) {
number = "4";
if (iTo == 32) {
letter = "a";
} else if (iTo == 33) {
letter = "b";
} else if (iTo == 34) {
letter = "c";
} else if (iTo == 35) {
letter = "d";
} else if (iTo == 36) {
letter = "e";
} else if (iTo == 37) {
letter = "f";
} else if (iTo == 38) {
letter = "g";
} else if (iTo == 39) {
letter = "h";
}
} else if ((iTo >= 40) && (iTo <= 47)) {
number = "3";
if (iTo == 40) {
letter = "a";
} else if (iTo == 41) {
letter = "b";
} else if (iTo == 42) {
letter = "c";
} else if (iTo == 43) {
letter = "d";
} else if (iTo == 44) {
letter = "e";
} else if (iTo == 45) {
letter = "f";
} else if (iTo == 46) {
letter = "g";
} else if (iTo == 47) {
letter = "h";
}
} else if ((iTo >= 48) && (iTo <= 55)) {
number = "2";
if (iTo == 48) {
letter = "a";
} else if (iTo == 49) {
letter = "b";
} else if (iTo == 50) {
letter = "c";
} else if (iTo == 51) {
letter = "d";
} else if (iTo == 52) {
letter = "e";
} else if (iTo == 53) {
letter = "f";
} else if (iTo == 54) {
letter = "g";
} else if (iTo == 55) {
letter = "h";
}
} else if ((iTo >= 56) && (iTo <= 63)) {
number = "1";
if (iTo == 56) {
letter = "a";
} else if (iTo == 57) {
letter = "b";
} else if (iTo == 58) {
letter = "c";
} else if (iTo == 59) {
letter = "d";
} else if (iTo == 60) {
letter = "e";
} else if (iTo == 61) {
letter = "f";
} else if (iTo == 62) {
letter = "g";
} else if (iTo == 63) {
letter = "h";
}
}
join = letter + number;
Log.d("Chess", join);
return false;
}
我认为您应该更改 onTouch 函数中的 returned 值,如果事件操作是 MotionEvent.ACTION_DOWN 您应该 return true 而不是 false 来获取其他事件。
我通过 MotionEvent.ACTION_MOVE 调用此方法,但值不会随着移动发生而改变。它只保留第一次触摸时按下的第一个值。这很可能与我在这里没有退出循环的 if 语句有关,但我可以借助一些帮助
public boolean onTouch(View v, MotionEvent event) {
//_gestureDetector.onTouchEvent(event);
long timeElapsed = 0L;
int eventAction = event.getAction();
String number = "";
String letter = "";
String join = "";
int iTo = getIndexOfButton(v);
// you may need the x/y location
int x = (int)event.getX();
int y = (int)event.getY();
String position = "";
// put your code in here to handle the event
switch (eventAction) {
case MotionEvent.ACTION_DOWN:
Log.d("Chess", "Down");
timeElapsed = event.getDownTime();
break;//return true;
case MotionEvent.ACTION_UP:
Log.d("Chess", "Up");
timeElapsed = event.getDownTime() - timeElapsed;
if (timeElapsed == 2000) {
//v.performClick();
Log.d("Chess", "perform click");
}
timeElapsed = 0L;
break;//return true;
case MotionEvent.ACTION_MOVE:
respondCoordinates(v);
}
// tell the View to redraw the Canvas
//invalidate();
return false;//super.onTouchEvent(event);
}
public Boolean respondCoordinates(View v){
int iTo = getIndexOfButton(v);
String join = "";
String letter = "";
String number = "";
//boolean moveActive = true;
if ((iTo >= 0) && (iTo <= 7)) {
number = "8";
if (iTo == 0) {
letter = "a";
} else if (iTo == 1) {
letter = "b";
} else if (iTo == 2) {
letter = "c";
} else if (iTo == 3) {
letter = "d";
} else if (iTo == 4) {
letter = "e";
} else if (iTo == 5) {
letter = "f";
} else if (iTo == 6) {
letter = "g";
} else if (iTo == 7) {
letter = "h";
}
} else if ((iTo >= 8) && (iTo <= 15)) {
number = "7";
if (iTo == 8) {
letter = "a";
} else if (iTo == 9) {
letter = "b";
} else if (iTo == 10) {
letter = "c";
} else if (iTo == 11) {
letter = "d";
} else if (iTo == 12) {
letter = "e";
} else if (iTo == 13) {
letter = "f";
} else if (iTo == 14) {
letter = "g";
} else if (iTo == 15) {
letter = "h";
}
} else if ((iTo >= 16) && (iTo <= 23)) {
number = "6";
if (iTo == 16) {
letter = "a";
} else if (iTo == 17) {
letter = "b";
} else if (iTo == 18) {
letter = "c";
} else if (iTo == 19) {
letter = "d";
} else if (iTo == 20) {
letter = "e";
} else if (iTo == 21) {
letter = "f";
} else if (iTo == 22) {
letter = "g";
} else if (iTo == 23) {
letter = "h";
}
} else if ((iTo >= 24) && (iTo <= 31)) {
number = "5";
if (iTo == 24) {
letter = "a";
} else if (iTo == 25) {
letter = "b";
} else if (iTo == 26) {
letter = "c";
} else if (iTo == 27) {
letter = "d";
} else if (iTo == 28) {
letter = "e";
} else if (iTo == 29) {
letter = "f";
} else if (iTo == 30) {
letter = "g";
} else if (iTo == 31) {
letter = "h";
}
} else if ((iTo >= 32) && (iTo <= 39)) {
number = "4";
if (iTo == 32) {
letter = "a";
} else if (iTo == 33) {
letter = "b";
} else if (iTo == 34) {
letter = "c";
} else if (iTo == 35) {
letter = "d";
} else if (iTo == 36) {
letter = "e";
} else if (iTo == 37) {
letter = "f";
} else if (iTo == 38) {
letter = "g";
} else if (iTo == 39) {
letter = "h";
}
} else if ((iTo >= 40) && (iTo <= 47)) {
number = "3";
if (iTo == 40) {
letter = "a";
} else if (iTo == 41) {
letter = "b";
} else if (iTo == 42) {
letter = "c";
} else if (iTo == 43) {
letter = "d";
} else if (iTo == 44) {
letter = "e";
} else if (iTo == 45) {
letter = "f";
} else if (iTo == 46) {
letter = "g";
} else if (iTo == 47) {
letter = "h";
}
} else if ((iTo >= 48) && (iTo <= 55)) {
number = "2";
if (iTo == 48) {
letter = "a";
} else if (iTo == 49) {
letter = "b";
} else if (iTo == 50) {
letter = "c";
} else if (iTo == 51) {
letter = "d";
} else if (iTo == 52) {
letter = "e";
} else if (iTo == 53) {
letter = "f";
} else if (iTo == 54) {
letter = "g";
} else if (iTo == 55) {
letter = "h";
}
} else if ((iTo >= 56) && (iTo <= 63)) {
number = "1";
if (iTo == 56) {
letter = "a";
} else if (iTo == 57) {
letter = "b";
} else if (iTo == 58) {
letter = "c";
} else if (iTo == 59) {
letter = "d";
} else if (iTo == 60) {
letter = "e";
} else if (iTo == 61) {
letter = "f";
} else if (iTo == 62) {
letter = "g";
} else if (iTo == 63) {
letter = "h";
}
}
join = letter + number;
Log.d("Chess", join);
return false;
}
我认为您应该更改 onTouch 函数中的 returned 值,如果事件操作是 MotionEvent.ACTION_DOWN 您应该 return true 而不是 false 来获取其他事件。