setting onclicklistener for several buttons

32 views Asked by At

i am trying to make tic-tac-toe game with android, and i am having a problem with setting onclicklisteners for several buttons

here is my code..

public class MainActivity extends Activity implements View.OnClickListener {

private Button button1;
private Button button2;
private Button button3;
private Button button4;
private Button button5;
private Button button6;
private Button button7;
private Button button8;
private Button button9;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    button1 = (Button) findViewById(R.id.first);
    button2 = (Button) findViewById(R.id.second);
    button3 = (Button) findViewById(R.id.third);
    button4 = (Button) findViewById(R.id.fourth);
    button5 = (Button) findViewById(R.id.fifth);
    button6 = (Button) findViewById(R.id.sixth);
    button7 = (Button) findViewById(R.id.seventh);
    button8 = (Button) findViewById(R.id.eighth);
    button9 = (Button) findViewById(R.id.ninth);

    button1.setOnClickListener(this);
    button2.setOnClickListener(this);
    button3.setOnClickListener(this);
    button4.setOnClickListener(this);
    button5.setOnClickListener(this);
    button6.setOnClickListener(this);
    button7.setOnClickListener(this);
    button8.setOnClickListener(this);
    button9.setOnClickListener(this);

}

public void onClick(View v) {

should i do this every time with every single button? or is there any way different than that?

thanks for your help!

0

There are 0 answers