Apologies for my title, I am having trouble properly articulating the problem.
I have seen OnCLickListener
implemented in two ways. The first is done by signifying that your class implements OnCLickListener
. The second accomplishes the task by having you declare it yourself.
Why in the first option can you simply put this
as your setOnCLickListener
argument, but in the second you must go through the trouble of creating the OnClickListener
object yourself?
The first:
public class WidgetConfig extends Activity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.widgetconfig);
Button b = (Button)findViewById(R.id.bwidgetconfig);
b.setOnClickListener(this);
}
//onClick defined outside of the onCreate
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
The Second:
public class WidgetConfig extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.widgetconfig);
Button b = (Button)findViewById(R.id.bwidgetconfig);
b.setOnClickListener(bListener);
}
private Button bListener = new OnClickListener(){
b.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
//TO-DO
}
});