Android _ android binds events to button controls in five ways. Android button controls

Source: Internet
Author: User

Android _ android binds events to button controls in five ways. Android button controls

I. Preface

Here, we will introduceBinding events to controls -- buttons in Android.

II. Specific implementation

First: directly bound to the Button control:

Step 1. Set android: onClick = "on the Button control. The property value corresponds to the method name in the MainActivity class (self-created method ):

            

Step 2. Create the corresponding method in the MainActivity class:

Public void demo (View view) {Toast. makeText (MainActivity. this, "the second button is clicked", Toast. LENGTH_SHORT). show ();}

 

 

     Method 2: Use an anonymous internal class:

Step 1. First, you need to get the Id specified in the Button control of the layout page in layout:

Step 2. Bind the listener to this button and use the anonymous internal class method. The Code is as follows:

Button = (Button) findViewById (R. id. button1); button. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View view ){
Toast. makeText (MainActivity. this, "using the anonymous internal class: The first button is clicked", Toast. LENGTH_SHORT). show ();}});

 

       

     

Third: Using External classes

Step 1. You need to get the Id specified in the Button control in the layout page (in MainActivity ):

Step 2. create a class, implement the OnClickListener interface, override the OnClick method in this interface, and create a Context attribute for this method (the Toast needs to be used later), and use the constructor to set the attribute value:

Package com. mqz. android_event_test; import android. content. context; import android. view. view; import android. view. view. onClickListener; import android. widget. toast; public class BtnTest implements OnClickListener {private Context context; public BtnTest (Context context) {this. context = context;} @ Override public void onClick (View view) {Toast. makeText (context, "OnClickListener interface implemented through external classes: the first button is clicked", Toast. LENGTH_SHORT ). show ();}}

 

Step 3. Bind the event to the obtained button and pass the current object

             

 

     

Method 4: Use MainActivity to directly implement the OnClickListener Interface

Step 1. Implement the OnClickListener interface in MainActivity and override the OnClick method:

Step 2. Bind the listener corresponding to the button to pass in the current object:

        

       Features:
1. In this way, the MainActivity class becomes the listener class, which is very simple.
2. However, this can easily lead to structure confusion, because the MainActivity class is mainly responsible for initializing the interface, which adds the event processor method and causes confusion.
3. The interface class must implement the listener method.  

          

Package com. mqz. android_event_test; import android. app. activity; import android. OS. bundle; import android. view. menu; import android. view. menuItem; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. toast; public class MainActivity extends Activity implements OnClickListener {private Button button;

@ Override public void onClick (View view ){

Toast. makeText (MainActivity. this, "OnClickListener interface implemented through MainActivity: The first button is clicked", Toast. LENGTH_SHORT ). show () ;}@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); button = (Button) findViewById (R. id. button2); button. setOnClickListener (this );}}

 

    

Method 5: bind a button event using the internal class of the member

Step 1. Obtain the Id of the Button control in the layout file:

              

Step 2. Create a member internal class in the MainActivity class, implement the OnClickListener interface, and override the OnClick method:

         

Class BtnTest1 implements OnClickListener {@ Override public void onClick (View view) {Toast. makeText (MainActivity. this, "passed the member internal class: the second button is clicked", Toast. LENGTH_SHORT ). show ();}}

Step 3. Bind related events to this button. The new internal class () does not need to be passed in, because this class is the internal class of the current class:

        Benefits:
1. The internal listener of the member can access all attributes of the external class. Therefore, the current object does not need to be passed in when the new OnClickListener implements the class object.
2. The internal listener of the member can reuse the external class because the internal listener of the member is the internal class of the external class.

 

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.