Reputation: 87763
This is an example of using outer class
public class MyActivity extends Activity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mylayout);
findViewById(R.id.myid).setOnClickListener(this));
}
public void onClick(View v){...}
}
This is an example of anonymous classes
public class MyActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mylayout);
findViewById(R.id.myid).setOnClickListener(new OnClickListener() {
public void onClick(View v){...}
}));
}
}
Upvotes: 3
Views: 1825
Reputation: 21984
Performance and efficiency are slightly a more important consideration in Android. Something considered to be half baked optimization effort, sometimes makes sense in android.(like we should not use enum but java int enum pattern). So to the answer to your question is.
If you have to register multiple onClick listeners, Implement interface and use switch case within it.
If you have to register only one on click listener, use anonymous class. (Android developers prefers anonymous class, whenever possible. Limiting the scope ;))
Upvotes: 5