Android buttons and android buttons
This document records four button-based click events.
First
Public class MainActivity extends Activity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // ------------ type 1 ---------------------- Button bt1 = (Button) findViewById (R. id. bt1); bt1.setOnClickListener (new MyListener ();} class MyListener implements OnClickListener {@ Override public void onClick (View v) {// TODO Auto-generated method stub System. out. println ("This is the first ");}}}
Second
Public class MainActivity extends Activity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // ------------ type 2 ---------------------- Button bt2 = (Button) findViewById (R. id. bt2); bt2.setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {// TODO Auto-generated method stub System. out. println ("this is the second type") ;}});} class MyListener implements OnClickListener {@ Override public void onClick (View v) {// TODO Auto-generated method stub System. out. println ("This is the first ");}}}
The Code volume is large and cannot be reused.
Third
Public class MainActivity extends Activity implements OnClickListener {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // ------------ Type 3 ---------------------- Button bt3 = (Button) findViewById (R. id. bt3); bt3.setOnClickListener (this) ;}@ Override public void onClick (View v) {// TODO Auto-generated method stub System. out. println ("this is the third type ");}}
Fourth
Add the following code to activity_main.xml:
<Button android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "fourth" android: onClick = "youyiyi"/>
Add the following code to MainActivity. java:
Public class MainActivity extends Activity implements OnClickListener {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main);} // ------------ type 4 ---------------------- // when a button is clicked, this method will be called public void youyiyi (View v) {// TODO Auto-generated method stub System. out. println ("this is the fourth type ");}}
The code structure is poor.
The complete code is as follows:
Activity_main.xml
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical" android: paddingBottom = "@ dimen/activity_vertical_margin" android: paddingLeft = "@ dimen/plugin" android: paddingRight = "@ dimen/plugin" android: paddingTop = "@ dimen/plugin" tools: context = ". mainActivity "> <Button android: id =" @ + id/bt1 "android: layout_width =" wrap_content "android: layout_height =" wrap_content "android: text = "first"/> <Button android: id = "@ + id/bt2" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "Type 2"/> <Button android: id = "@ + id/bt3" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "third"/> <Button android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "fourth" android: onClick = "youyiyi"/> </LinearLayout>
MainActivity. java
Package com. wuyudong. clickevent; import android. OS. bundle; import android. app. activity; import android. view. menu; import android. view. view; import android. view. view. onClickListener; import android. widget. button; public class MainActivity extends Activity implements OnClickListener {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // ------------ type 1 ---------------------- Button bt1 = (Button) findViewById (R. id. bt1); bt1.setOnClickListener (new MyListener (); // ------------ type 1 ---------------------- // ------------ type 2 ---------------------- Button bt2 = (Button) findViewById (R. id. bt2); bt2.setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {// TODO Auto-generated method stub System. out. println ("this is the second type") ;}}); // ------------ the second type ---------------------- // ------------ the third type -------------------- Button bt3 = (Button) findViewById (R. id. bt3); bt3.setOnClickListener (this); // ------------ class MyListener implements OnClickListener {@ Override public void onClick (View v) {// TODO Auto-generated method stub System. out. println ("This is the first type") ;}@ Override public void onClick (View v) {// TODO Auto-generated method stub System. out. println ("this is the third type");} // ------------ the fourth type -------------------- // when the button is clicked, this method will be called public void youyiyi (View v) {// TODO Auto-generated method stub System. out. println ("this is the fourth type ");}}