0

我在循环中创建 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,因为它们是循环变量。什么是替代方法。

4

2 回答 2

2
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;
                }
            });
        }
    }
于 2015-05-29T03:48:46.400 回答
0

我最终只是简单地将当前按钮的 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;
                }
            });
        }
    }
于 2015-05-29T23:36:21.903 回答