一个监听事件监听多个按钮
private Button btnWrite; private Button btnRead; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnRead = (Button) findViewById(R.id.btnRead); btnWrite = (Button) findViewById(R.id.btnWrite); btnRead.setOnClickListener(new ButtonListener()); btnWrite.setOnClickListener(new ButtonListener()); } private class ButtonListener implements OnClickListener { public void onClick(View v) { switch (v.getId()) { case R.id.btnRead: Toast.makeText(MainActivity.this, "读取", Toast.LENGTH_SHORT) .show(); break; case R.id.btnWrite: Toast.makeText(MainActivity.this, "写入", Toast.LENGTH_SHORT) .show(); break; default: break; } } }