这里提供另一种button按钮相关代码的写法:
private Button[]Btn= new Button[10];//用数组定义按钮 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //实例化按钮 Btn[1] = (Button)findViewById(R.id.btn_1); Btn[2] = (Button)findViewById(R.id.btn_2); Btn[3] = (Button)findViewById(R.id.btn_3); Btn[4] = (Button)findViewById(R.id.btn_4); Btn[5] = (Button)findViewById(R.id.btn_5); Btn[6] = (Button)findViewById(R.id.btn_6); Btn[7] = (Button)findViewById(R.id.btn_7); Btn[8] = (Button)findViewById(R.id.btn_8); Btn[9] = (Button)findViewById(R.id.btn_9); Btn[0] = (Button)findViewById(R.id.btn_0); //设置点击事件 for(int i = 0;i<10;i++){ Btn[i].setOnClickListener(this); }