1
package com.lzy.ButtonTestR; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class MainActivity extends Activity { public Button btn1=null; public Button btn2=null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btn2=(Button)findViewById(R.id.button2); btn2.setOnClickListener(listener); btn1=(Button)findViewById(R.id.button1); btn1.setOnClickListener(listener); } private OnClickListener listener =new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Button btn=(Button)v; switch (btn.getId()) { case R.id.button1: Toast.makeText(MainActivity.this, "按钮1被单击", Toast.LENGTH_LONG).show(); break; case R.id.button2: Toast.makeText(MainActivity.this, "按钮2被单击", Toast.LENGTH_LONG).show(); break; default: break; } } }; }
2
原文:http://www.cnblogs.com/SKeyC27/p/4095449.html