I have a pretty crappy book so I am not sure for their reasoning for using either form.
The first way that it would go about it would be like this
Button button = (Button) findViewById(R.id.btnButton)
button.setOnClickListener(new OnClickListener() {
//code code code }
Then our book wants to use Global Variables instead of local, so it randomly starts using a different way to go about things
Button btButton;
//Done as Global Variable.
btButton = (Button) findViewById(R.id.btnButton);
btButton.setOnClickListener(bButton);
Button.OnClickListener bButton = new Button.OnClickListener(){
//code }
It is things like this that my book is horrible at and one reason why I will not be buying the next installment of it. I hate when it just changes the way it does things without any real reason. So can anyone really tell me the difference here? Thanks.
As everyone says, there is no difference– but nobody has explained why.
ButtonextendsView.When you are using
onClickListener, you are using the one defined inView. Almost always, this one will work– it won’t withDialogs, and maybe some other classes.If you explicitly say
Button.onClickListener, you are referencing theonClickListenerdefined in theButtonclass, and sinceButtonextendsView, this is the same as using the genericonClickListener.There is also the inner anonymous vs explicit, but this is more of a preference. You will get better performance using a static onClickListener than in inner anonymous, simply because there is less garbage collection necessary, but you won’t notice it unless you have 30 or so anonymous inner listeners.