..


Sponsored Links

Add a listener to our button

In the previous chapter we have built the basic structure of the application by inserting a TextView and two buttons. In this chapter we will associate a listener object to etrambi cambiaremo buttons and the value inside the TextView depending on whether you clicked on the first or the second button.

As you might guess, there are various types of listeners, and each has the power to recognize and handle a particular user interaction with the screen of the device. In our case, since you want to handle the event "click on the button", we'll use a listener like OnClickListener.

The class OnClickListener, as can be seen on the official documentation of Android (to retrieve it, simply enter the name of the class in the search field), is an abstract class and therefore can not be directly instantiated without first redefining its methods.
Analyzing documentazone still see that the only method declared in class is the OnClick method that we have to redefine the instantiation time of the listener. In this method we have to insert the code for the intended behavior when the listener catches the event that it is associated.

Now open the file and HelloWorld.java, first, we import the following package:

 



 android.view.View imports;

 
which contains the class OnClickListener.

Once this is done after the code for elements declared in the file all'asociazione main.xml, sergeant insert the code:






 bottone1.setOnClickListener (View.OnClickListener new () {



  



 public void onClick (View view) {

 

    



 textView1.setText ('E' pressed button 1 ");



  



 }







 });



        





 bottone2.setOnClickListener (View.OnClickListener new () {



  



 public void onClick (View view) {

 

    



 textView1.setText ('E' pressed the button 2 ");



  



 }







 });



We are now going to analyze the code presented above. The first thing you associate with a bottone1 OnClickListener setOnClickListener using the method.

The reader, prababilmente, you will find a little 'confused by reading the structure of the code for the instantiation of' OnClickListener () because it is not very linear. The code, however, reflects what has been said a few paragraphs earlier about the necessity of having to redefine the onClick method. In fact, when we instantiate the 'OnClickListener with the new construct we also define the method by inserting the onClick behavior as updating the contents of the TextView.

Although the syntax shown above can be irksome to the reader that he should take it familiar as this is the only way to instantiate, properly, a listener. In fact, if we had tried to instantiate the 'OnClickListener and, after the method onCreate, to define the OnClick method we get only an error message from the compiler which indicates to us that you can not instantiate the class directly OnClickListener as abstract.

The code for the bottone2 is the same and it is deliberately omitted the explanation.

The method above, even if fully functional and logically correct, is not the best solution to be adopted as it requires a lot of code writing. The latter is also difficult to maintain if you have to do with so many elements that have an associated OnClickListener. In the next chapter we will provide, therefore, improved implementation of the listener.

Help develop applications for Android
E-Learning
AutoCAD (eBook) AutoCAD (eBook)
Creation of architectural structures. At only 29 €.
OpenOffice (Ebook) OpenOffice (Ebook)
The open-source software for managing the office work. Just 25 €.
PHP (Course) PHP (Course)
Full course for creating dynamic Web sites. From 49 €.
Sponsored Links