Specific implementation code:
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_main); // Method 1. Use a class that implements the OnClickListener interface((Button) findViewById(.btn1)) .setOnClickListener(new () { @Override public void onClick(View v) { Intent intent = new Intent(, ); startActivity(intent); } }); // Method 2. Adopt anonymous internal class((Button) findViewById(.btn2)) .setOnClickListener(new () { @Override public void onClick(View v) { Intent intent = new Intent(, ); startActivity(intent); } }); // Method 3. Activity directly implements the OnClickListener interface((Button) findViewById(.btn3)) .setOnClickListener(new () { @Override public void onClick(View v) { Intent intent = new Intent(, ); (intent); } }); // Method 4. Direct labeling trigger event((Button) findViewById(.btn4)) .setOnClickListener(new () { @Override public void onClick(View v) { Intent intent = new Intent(, ); (intent); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(, menu); return true; } @Override public 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 . int id = (); if (id == .action_settings) { return true; } return (item); }
In the above code:
1. The MainActivity class we have created needs to be inherited from Activity
2. You need to overwrite the onCreate method and load the corresponding layout (layout) file through the setContentView method.
3. Find the corresponding control (the control defined in the layout layout file) through the findViewById method and bind a Click event (implemented through a listener in Java, and implemented through delegate in C#)
4. You can pass data through Intent objects and jump to other activities
5. onCreateOptionsMenu and onOptionsItemSelected are methods when adding and selecting menu items.
Here are the contents of four activities:
The first type:
public class ButtonActivity1 extends Activity { Button button; @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_button1); button = (Button) findViewById(.btn1); (new MyListener()); } public class MyListener implements OnClickListener { @Override public void onClick(View v) { (, "This is the first way to write events, internal classes define events", 2000).show(); } } }
The second type:
public class ButtonActivity2 extends Activity { Button button ; @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_button2); button = (Button)findViewById(.btn1); (new () { @Override public void onClick(View v) { (, "This is the second way of writing events, the form of anonymous internal classes", 2000).show(); } }); } }
The third type:
public class ButtonActivity3 extends Activity implements OnClickListener { Button button; @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_button3); button= (Button)findViewById(.btn1); (this); } @Override public void onClick(View v) { switch (()) { case .btn1: (, "This is the third way of writing events, directly implementing the OnClick method of the OnClickListener interface", 2000).show(); break; default: (, "No trigger", 2000).show(); break; } } }
The fourth type:
You need to specify the btnClickEvent method in the layout layout file xml.
<LinearLayout xmlns:andro xmlns:tools="/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".Button4Activity" > <Button android: android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Click Me" android:onClick="btnClickEvent"/> </LinearLayout> public class ButtonActivity4 extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_button4); } public void btnClickEvent(View v){ (, "This is the fourth way of writing events, directly bind the Click event on the Button tag of the layout file.", 2000).show(); } }
Among the above four writing methods, the third method is relatively more used. When multiple buttons in an activity need to trigger a click event, it is easier to manage and maintain button event code through the third writing method.
Layout is a very important part of content. I will explain it in the blog below, which will be briefly mentioned here.
We use LinearLayout (linear layout, other relative layout, absolute layout, etc.), set the Android:orientation property value to vertical, and start displaying the control from top to bottom in turn.
The other three layout files are just like this content, and only one button is placed.
The configuration of activity_main.xml is as follows (just place 4 buttons):
<LinearLayout xmlns:andro xmlns:tools="/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="" > <Button android: android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/clickMe1" /> <Button android: android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/clickMe2" /> <Button android: android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/clickMe3" /> <Button android: android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/clickMe4" android:onClick="btnClickEvent"/> </LinearLayout>
A more important step in the end, you need to configure the registration activity in the file. The complete configuration is as follows:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:andro package="" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="14" /> <uses-permission android:name=".CALL_PHONE" /> <uses-permission android:name="" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="" /> <category android:name="" /> </intent-filter> </activity> <activity android:name=".ButtonActivity1" android:label="@string/button1" /> <activity android:name=".ButtonActivity2" android:label="@string/button2" /> <activity android:name=".ButtonActivity3" android:label="@string/button3" /> <activity android:name=".ButtonActivity4" android:label="@string/button4" /> </application> </manifest>
There is a thing to pay attention to here.
<action android:name="" />
Set MainActivity to "MainActivity", that is, which Activity is first displayed when starting.
The following activities need to be registered in the "cartoon file" so that these activities can be found in the program.
File configuration content:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">test</string> <string name="hello_world">Hello world!</string> <string name="action_settings">Settings</string> <string name="clickMe1">Button Event1</string> <string name="clickMe2">Button Event2</string> <string name="clickMe3">Button Event3</string> <string name="clickMe4">Button Event4</string> <string name="button1">Button1</string> <string name="button2">Button2</string> <string name="button3">Button3</string> <string name="button4">Button4</string> </resources>
Of course, you can also write it directly in the layout file, but this is more conducive to maintenance, which is also the recommended method for Android development.
The above are the four ways to write the activity jump button event in Android introduced to you. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!