public class MainActivity extends Activity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View num7 = findViewById(R.id.button1);
View num8 = findViewById(R.id.button2);
View num9 = findViewById(R.id.button3);
num7.setOnClickListener(this);
num8.setOnClickListener(this);
num9.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// 判断哪个按钮被点击
switch (v.getId()) {
case R.id.button1:
System.out.println("7被点击了");
Toast.makeText(this, "7被点击了", Toast.LENGTH_SHORT).show();
break;
case R.id.button2:
System.out.println("8被点击了");
Toast.makeText(this, "8被点击了", Toast.LENGTH_SHORT).show();
break;
case R.id.button3:
System.out.println("9被点击了");
Toast.makeText(this, "9被点击了", Toast.LENGTH_SHORT).show();
break;
}
}
public void haha(View view){
Toast.makeText(this, "2被点击了", Toast.LENGTH_SHORT).show();
}
}
|
|