佈局xmljava
<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" />
第一種,匿名內部類android
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(); } }); } }
第二種自定義單擊事件監聽類:ide
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測試的時候或者登錄界面之類的。this
按鈕多的狀況我仍是選擇第三種方法,方便。code