Android Learning Start-button buttons and event handling

Source: Internet
Author: User

Buttons and text boxes are relatively simple controls, the following main talk about the event response of the button, three ways (anonymous inner class response events, external class response events, this class of direct response events)

When you click the button, the "button is clicked" appears in the text box to see the effect first:

The following is the layout file for this interface:

1 <?XML version= "1.0" encoding= "Utf-8"?>2 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"3 Android:layout_width= "Match_parent"4 Android:layout_height= "Match_parent"5 android:orientation= "vertical" >6 7 <Button8 Android:id= "@+id/button1"9 Android:layout_width= "Match_parent"Ten Android:layout_height= "Wrap_content" One Android:text= "point Me" /> A  - <TextView - Android:id= "@+id/textview1" the Android:layout_width= "Match_parent" - Android:layout_height= "Wrap_content" - Android:hint= "Wait for click" /> <!--prompt message when not clicked by default - -  + </LinearLayout>

First, look at the first anonymous internal class write listener event handling method:

1  PackageCom.example.helloworld;2 3 4 Importandroid.app.Activity;5 ImportAndroid.os.Bundle;6 ImportAndroid.view.View;7 ImportAndroid.view.View.OnClickListener;8 ImportAndroid.widget.Button;9 ImportAndroid.widget.TextView;Ten  One  A  Public classMainactivityextendsActivity { -      -     PrivateButton btn1;//declaring button Objects the     PrivateTextView TV1;//Declaring a text box object -      - @Override -     protected voidonCreate (Bundle savedinstancestate) { +         Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.myactivity_main); +          A         //The following code handles the button click event in an anonymous internal way at          -         //Initializes an instance of two control objects and learns if MFC is familiar with the GetDlgItem API. -BTN1 =(Button) Findviewbyid (r.id.button1); -TV1 =(TextView) Findviewbyid (r.id.textview1); -          -         //here, the handler function for the button is clicked, which is implemented by the anonymous inner class of new Onclicklistener. inBtn1.setonclicklistener (NewOnclicklistener () { -              to             //An onclick method that overloads the Onclicklistener interface to handle the event after a hit + @Override -              Public voidOnClick (View v) { the                 //TODO auto-generated Method Stub *Tv1.sethint ("button clicked"); $             }Panax Notoginseng         }); -     } the}

Look at the external class of the writing, in fact, very simple, and then add a class:

1  PackageCom.example.helloworld;2 3 4 Importandroid.app.Activity;5 ImportAndroid.os.Bundle;6 ImportAndroid.view.View;7 ImportAndroid.view.View.OnClickListener;8 ImportAndroid.widget.Button;9 ImportAndroid.widget.TextView;Ten  One  A  Public classMainactivityextendsActivity { -      -     PrivateButton btn1;//declaring button Objects the     PrivateTextView TV1;//Declaring a text box object -      -     //this declares a way to access the TextView object. -      PublicTextView getTv1 () { +         returnTV1; -     } +  A @Override at     protected voidonCreate (Bundle savedinstancestate) { -         Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.myactivity_main); -          -         //The following code handles the button click event in a way other than a partial class -          in         //Initializes an instance of two control objects and learns if MFC is familiar with the GetDlgItem API. -BTN1 =(Button) Findviewbyid (r.id.button1); toTV1 =(TextView) Findviewbyid (r.id.textview1); +          -         //this uses the external class Myonclick to implement event handling theBtn1.setonclicklistener (NewMyonclick ( This)); *     } $ }Panax Notoginseng  -  the //external classes, implementing Onclicklistener interfaces + classMyonclickImplementsOnclicklistener { A  the     Privatemainactivity baseactivity; +      -     //the function of this constructor is primarily used to access the TextView object $      PublicMyonclick (mainactivity mt) { $Baseactivity =MT; -     } -      the @Override -      Public voidOnClick (View v) {Wuyi         //TODO auto-generated Method Stub theBASEACTIVITY.GETTV1 (). Sethint ("button clicked"); -     } Wu}

Finally, the activity class directly inherits the interface and implements the event response, which is also simple:

1  PackageCom.example.helloworld;2 3 4 Importandroid.app.Activity;5 ImportAndroid.os.Bundle;6 ImportAndroid.view.View;7 ImportAndroid.view.View.OnClickListener;8 ImportAndroid.widget.Button;9 ImportAndroid.widget.TextView;Ten  One  A //This class directly implements the interface Onclicklistener -  Public classMainactivityextendsActivityImplementsOnclicklistener { -      the     PrivateButton btn1;//declaring button Objects -     PrivateTextView TV1;//Declaring a text box object -      - @Override +     protected voidonCreate (Bundle savedinstancestate) { -         Super. OnCreate (savedinstancestate); + Setcontentview (r.layout.myactivity_main); A          at         //The following code handles the button click event in the same way that this class directly implements the interface -          -         //Initializes an instance of two control objects and learns if MFC is familiar with the GetDlgItem API. -BTN1 =(Button) Findviewbyid (r.id.button1); -TV1 =(TextView) Findviewbyid (r.id.textview1); -          in         //because this is the direct implementation of the interface, directly to the this pointer is the -Btn1.setonclicklistener ( This); to     } +  - @Override the      Public voidOnClick (View v) { *         //TODO auto-generated Method Stub $Tv1.sethint ("button clicked");Panax Notoginseng     } -}

OK, the onclick event of handling the button is just the implementation of the three kinds of Java writing, no technical content, with the deserve button other events can also be treated as above.

Android Learning Start-button buttons and event handling

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.