Android notes-how to write a Button click event? androidbutton
Button click events: they can be divided into the following types:
First, we will simply define an xml layout file with a Button.
Activity_main.xml:
<Button android: id = "@ + id/bt1" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "Click Event"/>
Then write the Java code.
MainActivity. java:
1. Anonymous internal class
@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); bt = (Button) findViewById (R. id. bt1); // 1. anonymous internal class bt. setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {// TODO Auto-generated method stub Log. I ("anonymous internal class", "Click Event ");}});
}
2. Define internal classes to implement the OnClickListener Interface
Public class MainActivity extends Activity {private Button bt; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); bt = (Button) findViewById (R. id. bt); bt. setOnClickListener (new MyListener ();} // defines the internal class and implements the OnClickListene Interface class MyListener implements OnClickListener {@ Override public void onClick (View v) {// TODO Auto-generated method stub Log. I ("defining internal classes and implementing OnClickListene interfaces", "click events ");}}
}
3. defined Constructor
Public class MainActivity extends Activity {private Button bt; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); bt = (Button) findViewById (R. id. bt); myListener () ;}// define the constructor private void myListener () {// TODO Auto-generated method stub bt. setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {// TODO Auto-generated method stub Log. I ("define constructor", "Click Event ");}});}}
4. Use Activity to implement the OnClickListener Interface
Public class MainActivity extends Activity implements OnClickListener {private Button bt; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); bt = (Button) findViewById (R. id. bt); bt. setOnClickListener (this);} // use Activity to implement the OnClickListener interface @ Override public void onClick (View v) {// TODO Auto-generated method stub Log. I ("using Activity to implement OnClickListener interface", "Click Event ");}}
5. Specify the onClick attribute of the Button:
Specify the onClick attribute in the layout file, and then implement the onButtonClick method in the Activity.
Layout file:
<Button android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: onClick = "click" android: text = "click Event"/>
Java code:
Public class MainActivity extends Activity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main);} // 5 specify the onClick attribute in public void click (View v) {// TODO Auto-generated method stub Log. I ("specify onClick attribute method", "Click Event ");}}
In addition, if you click multiple buttons, you can also specify the onClick attribute to obtain the resource id. The resource id can be used to determine which button the user has clicked.
Layout file:
<Button android: id = "@ + id/bt1" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: onClick = "click" android: text = "Click Event 1"/> <Button android: id = "@ + id/bt2" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: onClick = "click" android: text = "click Event 2"/> <Button android: id = "@ + id/bt3" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: onClick = "click" android: text = "click Event 3"/> <Button android: id = "@ + id/bt4" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: onClick = "click" android: text = "click Event 4"/>
Java code:
Public class MainActivity extends Activity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main);} // specifies the onClick attribute method. // specifies the View object of the uploaded object, which is the public void click (View v) button object that the user presses) {// TODO Auto-generated method stub // obtain the resource id of the component int id = v. getId (); switch (id) {case R. id. bt1: Log. I ("specify onClick attribute method", "bt1 Click Event"); break; case R. id. bt2: Log. I ("specify onClick attribute method", "bt2 Click Event"); break; case R. id. bt3: Log. I ("specify onClick attribute method", "bt3 Click Event"); break; case R. id. bt4: Log. I ("specify onClick attribute method", "bt4 Click Event"); break; default: break ;}}}
Effect