Please explain why, in the below code (this)
is used as argument to setOnClickListener()
? I am new in coding if you could explain the total code I would be thankful.
public class MainActivity extends Appcomatactivity implements
View.OnClickListner {
Textview textview;
Button push_me, push_me2;
protected void onCreate(bundle savedInstance) {
super.onCreate(savedInstance);
setContentView(R.Layout.activity_main);
textview = (Textview)findViewById(R.Id.Textview);
push_me=(Button)findViewById(R.Id.pushmebutton);
push_me2=(Button)findViewById(R.Id.pushmebutton2);
// Why does below code use (this) as argument?
push_me.setOnClickListener(this);
push_me2.setOnClickListener(this);
}
// <...some activity methods...>
// onclick method defined:
public void onClick(View view) {
switch(view.getid()) {
case R.Id.pushmebutton:
textview.setText("button 1 clicked");
break;
case R.id.pushmebutton2:
textview.setText("button 2 clicked");
break;
}
}
}