Summary of Android button event 5, android 5
Five buttons:
1. Anonymous internal class
2. Class implementation View. OnClickListener Interface
3. Create an instantiated interface object
4. Use internal classes
5. Configure the Android: onclick attribute in a custom Method
Import android. media. jetPlayer. onJetEventListener; import android. OS. bundle; import android. app. activity; import android. app. alertDialog; import android. app. alertDialog. builder; import android. app. dialog; import android. content. dialogInterface; import android. view. menu; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. imageButton; import android. widget. toast; public class MainActivity extends Activity implements OnClickListener {Button button1, button2, button4, button5; ImageButton button3; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); button1 = (Button) findViewById (R. id. button1); button1.setOnClickListener (new OnClickListener () {// Method 1: Click the event @ Overridepublic void onClick (View arg0) implemented inside an anonymous user) {// TODO Auto-generated method stubToast. makeText (MainActivity. this, "click events implemented in the anonymous internal", Toast. LENGTH_SHORT ). show () ;}}); button2 = (Button) findViewById (R. id. button2); button2.setOnClickListener (this); // The third method is to create an instantiated interface object button3 = (ImageButton) findViewById (R. id. button3); OnClickListener listener = new OnClickListener () {@ Overridepublic void onClick (View arg0) {// TODO Auto-generated method stubToast. makeText (MainActivity. this, "Create an instantiated interface object", Toast. LENGTH_SHORT ). show () ;}}; button3.setOnClickListener (listener); // Method 4: Use the internal class button4 = (Button) findViewById (R. id. button4); button4.setOnClickListener (new MyOnClickListener ();} @ Overridepublic boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true;} // Method 2: View. onClickListener interface onClick () method @ Overridepublic void onClick (View arg0) {// TODO Auto-generated method stubswitch (arg0.getId () {case R. id. button2: Toast. makeText (this, "implements View. onClickListener interface implements the onClick () method in the interface ", Toast. LENGTH_SHORT ). show (); break;} class MyOnClickListener implements OnClickListener {@ Overridepublic void onClick (View arg0) {// TODO Auto-generated method stubToast. makeText (MainActivity. this, "internal class method", Toast. LENGTH_SHORT ). show () ;}// Method 5: configure the android: onClick attribute public void onButton (View view) {switch (view. getId () {case R. id. button5: Toast. makeText (this, "custom method, configure the android: onClick attribute", Toast. LENGTH_SHORT ). show (); break ;}} public void onButton7 (View view) {Toast. makeText (this, "aaaaaaaaaaaaaaaaaaa", Toast. LENGTH_SHORT ). show (); setContentView (R. layout. activity_main2 );}}