1

I am currently fooling around with the android SDK (eclipse) and i am trying to make a simple button-based TicTacToe game.

I made an array where i save the Buttons and i am trying to make the ClickEvents dynamically:

Here is the Code (Which works so far):

public class MainActivity extends Activity 
{
final Button[] buttons = new Button[9];
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    buttons[0] = (Button) findViewById(R.id.button1);
    buttons[1] = (Button) findViewById(R.id.button2);
    buttons[2] = (Button) findViewById(R.id.button3);
    buttons[3] = (Button) findViewById(R.id.button4);
    buttons[4] = (Button) findViewById(R.id.button5);
    buttons[5] = (Button) findViewById(R.id.button6);
    buttons[6] = (Button) findViewById(R.id.button7);
    buttons[7] = (Button) findViewById(R.id.button8);
    buttons[8] = (Button) findViewById(R.id.button9);

    for(int i = 0;i < 9;i++)
    {
        buttons[i].setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View arg0)
            {
                // 
            }
        });
    }
}

}

The problem comes when i try to reference to a button in the onClick-Method using 'i'. For example:

            @Override
            public void onClick(View arg0)
            {
                             //buttons[0].setText("X"); works by the way
                buttons[i].setText("X");

            }

Eclipse tells me: "Change modifier of 'i' to final"

Well but if 'i' would be final, i wouldn't be able to increase 'i' in the loop.

So i tried this:

            @Override
            public void onClick(View arg0)
            {
                            final int position = i;
                buttons[position].setText("X");
            }

Which ends up resulting with the same Error "Change modifier of 'i' to final"

So my Question is: How can i make this work?

I have tried google but i wasn't able to find anything and this really is the first time i ever encountered this problem .

Hope you guys can help me out!


EDIT:

Well the solution was simple, here is the now working code:

        for(int i = 0;i < 9;i++)
    {
        final int position = i;
        buttons[i].setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View arg0)
            {
                buttons[position].setText("X");
            }
        });
    }
4

3 回答 3

2

你可以这样做:

for(int i = 0;i < 9;i++)
   {

      final int position = i;

      buttons[i].setOnClickListener(new View.OnClickListener()
      {
         @Override
         public void onClick(View arg0)
         {
            buttons[position].setText("X"); 
         }
      });
}
于 2013-06-16T08:31:40.620 回答
1

在您的循环中创建另一个最终 int var 将其设置为与 i 相同的值,并从您的侦听器中引用此 var。

for(int i = 0;i < 9;i++)
{
    final int index = i;
    buttons[i].setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View arg0)
        {
            buttons[index].setText("X");
        }
    });
}
于 2013-06-16T08:30:29.387 回答
0

有一个单独的类来实现View.OnClickListener并有一个构造函数来接受index value

于 2013-06-16T08:37:04.533 回答