为几个按钮设置onclicklistener

时间:2014-11-15 02:39:57

标签: android view onclicklistener tic-tac-toe

我正在尝试用android制作井字游戏, 我在为几个按钮设置onclicklisteners时遇到问题

这是我的代码..

公共类MainActivity extends Activity实现了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) {

每次按下按钮我都应该这样做吗?还是有什么不同的呢?

感谢您的帮助!

0 个答案:

没有答案