C # Four programming languages for programmers to learn about Button events in the Android Development Series
After the preparation of the first two blogs, let's warm up today and make a simple example.
The directory structure still references the previous blog.
Specific implementation code:
Public class MainActivity extends Activity {@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // method 1. use the OnClickListener Interface Class (Button) findViewById (R. id. btn1 )). setOnClickListener (new View. onClickListener () {@ Overridepublic void onClick (View v) {Intent intent = new Intent (MainActivity. this, Button1Activity. class); startActivity (intent) ;}}); // method 2. use an anonymous internal class (Button) findViewById (R. id. btn2 )). setOnClickListener (new View. onClickListener () {@ Overridepublic void onClick (View v) {Intent intent = new Intent (MainActivity. this, Button2Activity. class); startActivity (intent) ;}}); // method 3. activity directly implements the OnClickListener interface (Button) findViewById (R. id. btn3 )). setOnClickListener (new View. onClickListener () {@ Overridepublic void onClick (View v) {Intent intent = new Intent (MainActivity. this, Button3Activity. class); MainActivity. this. startActivity (intent) ;}}); // Method 4. tag directly label trigger event (Button) findViewById (R. id. btn4 )). setOnClickListener (new View. onClickListener () {@ Overridepublic void onClick (View v) {Intent intent = new Intent (MainActivity. this, Button4Activity. class); MainActivity. this. startActivity (intent) ;}}) ;}@ 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 ;}@ Overridepublic boolean onOptionsItemSelected (MenuItem item) {// Handle action bar item clicks here. the action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml.int id = item. getItemId (); if (id = R. id. action_settings) {return true;} return super. onOptionsItemSelected (item );}}
The above code has 5 points to note:
1. The MainActivity class we created must inherit from the Activity
2. Override the onCreate method and use the setContentView method to load the corresponding layout (layout) file.
3. Use the findViewById method to find the corresponding control (the control defined in the layout file) and bind a Click event (implemented by the listener in Java and implemented by delegation in C)
4. You can pass data through the Intent object and jump to another Activity.
5. onCreateOptionsMenu and onOptionsItemSelected are the methods used to add and select menu items.
The first method to trigger an event using the Button1Activity demo button is as follows:
Public class Button1Activity extends Activity {Button button; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_button1); button = (Button) findViewById (R. id. btn1); button. setOnClickListener (new MyListener ();} public class MyListener implements OnClickListener {@ Overridepublic void onClick (View v) {Toast. makeText (Button1Activity. this, "this is the first way to write events, internal class definition events", 2000 ). show ();}}}
Button2Activity:
Public class Button2Activity extends Activity {Button button; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_button2); button = (Button) findViewById (R. id. btn1); button. setOnClickListener (new View. onClickListener () {@ Overridepublic void onClick (View v) {Toast. makeText (Button2Activity. this, "this is the second way of writing events, in the form of anonymous internal classes", 2000 ). show ();}});}}
The third way to use the Button3Activity demo button to trigger an event:
Public class Button3Activity extends Activity implements OnClickListener {Button button; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_button3); button = (Button) findViewById (R. id. btn1); button. setOnClickListener (this) ;}@ Overridepublic void onClick (View v) {switch (v. getId () {case R. id. btn1: Toast. makeText (Button3Activity. this, "this is the third method of the event, which implements the OnClick method of the OnClickListener interface", 2000 ). show (); break; default: Toast. makeText (Button3Activity. this, "not triggered", 2000 ). show (); break ;}}}
The fourth way to use the Button4Activity demo button to trigger an event:
Public class Button4Activity extends Activity {@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_button4);} public void btnClickEvent (View v) {Toast. makeText (Button4Activity. this, "this is the fourth way to write events. Bind the Click event directly to the Button tag of the layout file", 2000 ). show ();}}
In this way, you must specify the btnClickEvent method in the layout file.
It should be noted that the 3rd methods in the above four writing methods are more commonly used. It can be imagined that multiple buttons in an activity need to trigger the click event, and it is easier to manage and maintain the button Event code by using 3rd methods.
Layout is an important part. I will explain it in the following blog, which will be briefly mentioned here.
We use LinearLayout (linear layout, relative layout, absolute layout, etc.), set the android: orientation attribute value to vertical (vertical), and display the control from top to bottom.
The other three layout files share the same content, and only one button is placed.
The configuration of activity_main.xml is as follows (four buttons are simply placed ):
In the end, the more important step is to configure the registration Activity in the AndroidManifest. xml file. The complete configuration is as follows:
There is something worth attention here,
Set MainActivity to "main Activity", that is, the Activity that is first displayed at startup.
The following activities must be registered in the "list file" so that these activities can be found in the program.
Content configured in the strings. xml file:
Test
Hello world!
Settings
Button Event 1
Button event 2
Button event 3
Button event 4
Button 1
Button 2
Button 3
Button 4
Of course, you can also write it directly in the layout file, but this is more conducive to maintenance, which is also recommended by Android development.
Finally, the program runs in the simulator: