在 setOnTouchListener 中访问循环变量
Access Loop Variables within setOnTouchListener
我正在循环创建 OnTouchListeners:
for(int i = 0; i < buttons.length; i++){
for(int j = 0; j < buttons[i].length; j++ ){
String buttonID = "button" + i + j;
int resID = getResources().getIdentifier(buttonID, "id", getPackageName());
buttons[i][j] = (Button) findViewById(resID);
buttons[i][j].setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
if (arg1.getAction() == MotionEvent.ACTION_DOWN){
socket.emit("button down", "button" + i + j);
return false;
}
if (arg1.getAction() == MotionEvent.ACTION_UP){
socket.emit("button up", "button" + i + j);
return false;
}
return true;
}
});
}
}
如何访问 setOnTouchListener 函数中的变量 i 和 j。我不能将它们声明为 final,因为它们是循环变量。有什么替代方法。
for(int i = 0; i < buttons.length; i++){
final int iNow = i;
for(int j = 0; j < buttons[i].length; j++ ){
final int jNow = j;
String buttonID = "button" + i + j;
int resID = getResources().getIdentifier(buttonID, "id", getPackageName());
buttons[i][j] = (Button) findViewById(resID);
buttons[i][j].setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
if (arg1.getAction() == MotionEvent.ACTION_DOWN){
socket.emit("button down", "button" + iNow + jNow);
return false;
}
if (arg1.getAction() == MotionEvent.ACTION_UP){
socket.emit("button up", "button" + iNow + jNow);
return false;
}
return true;
}
});
}
}
我最终只是在循环中获取了当前按钮的 ID:
for(int i = 0; i < buttons.length; i++){
for(int j = 0; j < buttons[i].length; j++ ){
String buttonID = "button" + i + j;
int resID = getResources().getIdentifier(buttonID, "id", getPackageName());
buttons[i][j] = (Button) findViewById(resID);
buttons[i][j].setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
if (arg1.getAction() == MotionEvent.ACTION_DOWN){
socket.emit("button down", getResources().getResourceEntryName(arg0.getId()));
return false;
}
if (arg1.getAction() == MotionEvent.ACTION_UP){
socket.emit("button up", getResources().getResourceEntryName(arg0.getId()));
return false;
}
return true;
}
});
}
}
我正在循环创建 OnTouchListeners:
for(int i = 0; i < buttons.length; i++){
for(int j = 0; j < buttons[i].length; j++ ){
String buttonID = "button" + i + j;
int resID = getResources().getIdentifier(buttonID, "id", getPackageName());
buttons[i][j] = (Button) findViewById(resID);
buttons[i][j].setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
if (arg1.getAction() == MotionEvent.ACTION_DOWN){
socket.emit("button down", "button" + i + j);
return false;
}
if (arg1.getAction() == MotionEvent.ACTION_UP){
socket.emit("button up", "button" + i + j);
return false;
}
return true;
}
});
}
}
如何访问 setOnTouchListener 函数中的变量 i 和 j。我不能将它们声明为 final,因为它们是循环变量。有什么替代方法。
for(int i = 0; i < buttons.length; i++){
final int iNow = i;
for(int j = 0; j < buttons[i].length; j++ ){
final int jNow = j;
String buttonID = "button" + i + j;
int resID = getResources().getIdentifier(buttonID, "id", getPackageName());
buttons[i][j] = (Button) findViewById(resID);
buttons[i][j].setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
if (arg1.getAction() == MotionEvent.ACTION_DOWN){
socket.emit("button down", "button" + iNow + jNow);
return false;
}
if (arg1.getAction() == MotionEvent.ACTION_UP){
socket.emit("button up", "button" + iNow + jNow);
return false;
}
return true;
}
});
}
}
我最终只是在循环中获取了当前按钮的 ID:
for(int i = 0; i < buttons.length; i++){
for(int j = 0; j < buttons[i].length; j++ ){
String buttonID = "button" + i + j;
int resID = getResources().getIdentifier(buttonID, "id", getPackageName());
buttons[i][j] = (Button) findViewById(resID);
buttons[i][j].setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
if (arg1.getAction() == MotionEvent.ACTION_DOWN){
socket.emit("button down", getResources().getResourceEntryName(arg0.getId()));
return false;
}
if (arg1.getAction() == MotionEvent.ACTION_UP){
socket.emit("button up", getResources().getResourceEntryName(arg0.getId()));
return false;
}
return true;
}
});
}
}