Implements OnClickListener VS. new Button.OnClickListener () {};

I have a question about implementing OnClickListeners for development using ADT. I'm not sure which way is more efficient, can someone provide me with pro and con for each approach?

class x extends Activity implements OnClickListener { button.SetOnClickListener(this); OnclickListener(View v) { switch(v.getGetId()); { case R.id.y: //do stuff here break; . . . } } } 

<-VERSUS →

 class a extends Activity { . . . btn.setOnClickListener(new Button.OnClickListener() { OnClickListener(View v) { //do stuff here } }); } 
+7
java android
source share
1 answer

I think this is mainly a case of personal preference. Any performance difference is likely to be negligible.

Personally, I prefer a nested class:

  • Its harder to screw
  • The switch statements are ugly
  • You can use local variables that may be useful.

But some people think that nested classes are ugly and therefore prefer an implementation approach. This approach works better if you have only one listener implemented in action.

+8
source share

All Articles