Anonymous classes
In Chapter 12, The Stack, the Heap, and the Garbage Collector, we briefly introduced anonymous classes. Here, we will discuss it in a little more detail and examine how they can help us. When a RadioButton
widget is part of a RadioGroup
widget, the visual appearance of them all is coordinated for us. All we need to do is react when any given RadioButton
widget is pressed. Of course, as with any other button, we need to know when they have been clicked on.
A RadioButton
widget behaves differently to a regular Button
, and simply listening for clicks in onClick
(after implementing OnClickListener
) will not work because RadioButton
is not designed that way.
What we need to do is use another Java feature. We need to implement a class, that is, an anonymous class, for the sole purpose of listening for clicks on the RadioGroup
widget. The next block of code assumes that we have a reference to a RadioGroup
widget, called radioGroup
. Here is the code:
radioGroup...