布局xml
<Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button1" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button2" />
第一种,匿名内部类
public class TestButtonActivity extends Activity { Button btn1, btn2; Toast tst; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_test_button); btn1 = (Button) findViewById(R.id.button1); btn2 = (Button) findViewById(R.id.button2); btn1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Toast tst = Toast.makeText(TestButtonActivity.this, "111111111", Toast.LENGTH_SHORT); tst.show(); } }); btn2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Toast tst = Toast.makeText(TestButtonActivity.this, "222222222", Toast.LENGTH_SHORT); tst.show(); } }); } }
第二种自定义单击事件监听类:
public class TestButtonActivity extends Activity { Button btn1, btn2; Toast tst; class MyClickListener implements OnClickListener { @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.button1: tst = Toast.makeText(TestButtonActivity.this, "111111111", Toast.LENGTH_SHORT); tst.show(); break; case R.id.button2: tst = Toast.makeText(TestButtonActivity.this, "222222222", Toast.LENGTH_SHORT); tst.show(); break; default: break; } } } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_test_button); btn1 = (Button) findViewById(R.id.button1); btn2 = (Button) findViewById(R.id.button2); btn1.setOnClickListener(new MyClickListener()); btn2.setOnClickListener(new MyClickListener()); } }
第三种 Activity继承View.OnClickListener,由Activity实现OnClick(View view)方法,在OnClick(View view)方法中用switch-case对不同id代表的button进行相应的处理
public class TestButtonActivity extends Activity implements OnClickListener { Button btn1, btn2; Toast tst; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_test_button); btn1 = (Button) findViewById(R.id.button1); btn2 = (Button) findViewById(R.id.button2); btn1.setOnClickListener(this); btn2.setOnClickListener(this); } @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.button1: tst = Toast.makeText(this, "111111111", Toast.LENGTH_SHORT); tst.show(); break; case R.id.button2: tst = Toast.makeText(this, "222222222", Toast.LENGTH_SHORT); tst.show(); break; default: break; } } }
第四种 在XML文件中“显示指定按钮的onClick属性,这样点击按钮时会利用反射的方式调用对应Activity中的click()方法
<Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onClick" android:text="Button1" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onClick" android:text="Button2" />
public class TestButtonActivity extends Activity { Button btn1, btn2; Toast tst; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_test_button); } // 注意 这里没有 @Override 标签 public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.button1: tst = Toast.makeText(this, "111111111", Toast.LENGTH_SHORT); tst.show(); break; case R.id.button2: tst = Toast.makeText(this, "222222222", Toast.LENGTH_SHORT); tst.show(); break; default: break; } } }
粗略总结一下,就是按钮少的时候用匿名内部类会比较快,比如写demo测试的时候或者登陆界面之类的。
按钮多的情况我还是选择第三种方法,方便。