..
In the previous chapter was not deliberately offer the reader an optimized version of the listener, because it was preferred to first explain the basics of operation in order to avoid confusion and to follow a linear path of exposure. From a standpoint implemented, however, the code outlined above - as said - is certainly not the preferred solution.
What we see now, therefore, is an optimized implementation of our listeners by creating an object to be invoked on both OnClickListener buttons using the method setOnClickListener.
Posizioniamoci, then touch the file HelloWorld.java and insert the following code:
View.OnClickListener View.OnClickListener manager = new () {
public void onClick (View view) {
switch (view.getId ()) {
R.id.bottone1 homes:
textView1.setText ('E' was clicked button 1 !!!!!");
break;
R.id.bottone2 homes:
textView1.setText ('E' button was clicked 2 !!!!!");
break;
}
}
};
bottone1.setOnClickListener (manager);
bottone2.setOnClickListener (manager);
So, first you create an object of type OnClickListener and, after the new construct is called the constructor for the class then going to redefine the OnClick method as we did previously.
The listener that we have created has the distinction of being the same for both bottonni and therefore, than the previous implementation, we saved the memory on a new instance of a listener. The advantage is even more evident as the number of buttons in the window.
In essence, the listener is able to guess which of two buttons was clicked acting accordingly. The recognition of the button is pressed using a switch based on the condition view.getId ().
As you can see the OnClick method has as argument a View and then, when a button is clicked, the View object is initialized with the button pressed. Using the method getId () return the unique id associated to the button in the file R.java we saw in previous chapters.
At this point it is possible to distinguish the button pressed on the basis of the id of the button, as we can see in the case construct.
Once the code for the OnClick method is invoked on both the method setOnClickListener buttons as an argument precisely the 'OnClickListener created earlier.
Once understood, the code shown in this chapter, it is easy to use and easy to change. In fact, this implementation, as well as allowing less use of memory (but not least when programming for devices with very limited memory such as a phone) has the great advantage of being easily maintainable. This is because if we need to change the behavior of the listener will know precisely what part of the code go to edit. Same thing you can not say if they use the old implementation because, in that case, we should go through all the code for every element that's used for a listener then andarne to modify the code inside.
The improved implementation is also easily expandable as if we had the need to insert another button that uses the same listener will have to follow the following steps:
| |
ASP (Advanced)
Full course for creating dynamic Web sites. From 39 €. |
| |
Javascript (Course)
Complete guide to client-side scripting. From 39 €. |
| |
PHP (Course)
Full course for creating dynamic Web sites. From 49 €. |