4 ways for C # Programmers to learn button events for Android development series

Source: Internet
Author: User

After the first two blog to pave the way, we warm up today, to do a simple example.

directory structure or reference to 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 a Class (Button) Findviewbyid (R.ID.BTN1) that implements the Onclicklistener interface. Setonclicklistener (new View.onclicklistener () {@ overridepublic void OnClick (View v) {Intent Intent = new Intent (mainactivity.this,button1activity.class); startactivity (intent);}); /Method 2. Use anonymous inner 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. The 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. The label directly labels the 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 PR Esent.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 and RoidManifest.xml.int id = item.getitemid (); if (id = = r.id.action_settings) {return true;} return super.onoptionsitemselected (item);}}
The above code has 5 points to explain:

1. The Mainactivity class we build needs to inherit from activity

2, need to overwrite the OnCreate method, and through the Setcontentview method to load the corresponding layout (layout) file

3. Find the appropriate control (the control defined in the layout file) by Findviewbyid method and bind a click event (implemented in Java through a listener, implemented by a delegate in C #)

4, can pass the data through the intent object and jump to other activity

5, Oncreateoptionsmenu, and onoptionsitemselected are methods for adding and selecting menu items.

Button1activity the first way to show the button triggering event:

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 notation of the event, the inner class defines the event", ". Show ();}}}

Button2activity the second way to show the button triggering event:

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 notation of the event, the form of an anonymous inner class,"). Show ();}});}}

Button3activity the third way to show the button triggering 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 form of the event, the direct implementation of the Onclicklistener interface onclick method ",". Show (); Break;default:toast.maketext (Button3activity.this, " No trigger ","). Show (); break;}}}

Button4activity the fourth way to show the button triggering 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 notation of the event, bind the click event directly to the button label of the layout file", (). Show ();}}

This notation requires the Btnclickevent method to be specified in the layout file.

<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"    tools:context= "com.example.test.Button4Activity" >    <button        android:id= "@+id/btn1"        android:layout_width= "match_parent"        android:layout_height= "Wrap_content"        android:text= "Click Me"         android:onclick= "Btnclickevent"/></linearlayout>

It is important to note that the 3rd method in the above 4 notation is a bit more relative to the other. You can imagine that there are multiple buttons in an activity that trigger the Click event, and the 3rd way makes it easier to manage and maintain the button event code.

Layout is a very important piece of content, I will explain in the following blog, here is a brief mention.

We use LinearLayout (linear layout, other relative layouts, absolute layouts, etc.), set the Android:orientation property value to Vertical (vertical), and then start the control from the top down.

3 Other layout files just like this, there's only one button placed.

The configuration of the Activity_main.xml is as follows (4 buttons are simply placed):

<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:orien tation= "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=" com.example.test.MainActivity "> <button android:id=" @+id/btn1 " Android:layout_width= "Match_parent" android:layout_height= "wrap_content" android:text= "@string/clickme1"/&G    T <button android:id= "@+id/btn2" android:layout_width= "match_parent" android:layout_height= "Wrap_con Tent "android:text=" @string/clickme2 "/> <button android:id=" @+id/btn3 "android:layout_width = "Match_parent" android:layout_height= "wrap_content "android:text=" @string/clickme3 "/> <button android:id=" @+id/btn4 "Android:layout_w Idth= "Match_parent" android:layout_height= "wrap_content" android:text= "@string/clickme4"/></linearlay Out>

The final more important step is to configure the registered activity in the Androidmanifest.xml file, complete with the following configuration:

<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/        Android "package=" Com.example.test "android:versioncode=" 1 "android:versionname=" 1.0 "> <uses-sdk android:minsdkversion= "android:targetsdkversion="/> <uses-permission android:name= "Android.permis" Sion. Call_phone "/> <uses-permission android:name=" Android.permission.INTERNET "/> <application androi D:allowbackup= "true" android:icon= "@drawable/ic_launcher" android:label= "@string/app_name" android:th Eme= "@style/apptheme" > <activity android:name= ". Mainactivity "android:label=" @string/app_name "> <intent-filter> <action Android:name= "Android.intent.action.MAIN"/> <category android:name= "Android.intent.category.LAUNCHER "/> </intent-filter> </activity> <activity android:name= ". Button1activity "android:label=" @string/button1 "/> <activity android:name=". Button2activity "android:label=" @string/button2 "/> <activity android:name=". Button3activity "android:label=" @string/button3 "/> <activity android:name=". Button4activity "android:label=" @string/button4 "/> </application></manifest>

There's a place to be aware of this,
<action android:name= "Android.intent.action.MAIN"/>

Set Mainactivity to "main activity", which activity is displayed first at startup.

The following multiple activity needs to be registered in the manifest file so that the activity can be found in the program.

Contents of the Strings.xml file configuration:

<?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 events 1</string>    <string name= "ClickMe2" > button Event 2</string>    <string name= "clickMe3" > button event 3</string>    <string name= "ClickMe4" > button Event 4</string>    <string name= "Button1" > button 1</string> <string    name= "Button2" > Button 2</string>    <string name= "Button3" > button 3</string> <string    name= "button4" > button 4< /string></resources>
Of course you can also write directly to the layout file, but this is more conducive to maintenance, which is the recommended method of Android development.

Finally, the program runs in the emulator:







Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.