I don’t understand why I have to implement the OnClickListener to use the OnClick-method. Assuming this code:
public class KlickitestActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
public void onClick(View v) {
// code what happens when a click is made
}
From the class OnClickListener I only use the method onClick(View v) – and this one is overwritten. Why can’t I just define the onClick-method without implementing the OnClickListener?
You can. You can do that by using an Anonymous Inner Class :
However implementing an OnClickListener makes it easier to handle events, and improves code readability. i.e You can use one Listener method, and passing a
Viewto handle multiple buttons/listeners with a switch statement, something similar to :