I'm new to Android environment I'm just wondering why does the OnClick(View v)
method accepts this
if the required parameter is in type View
and my method extends Activity
? Is there a relationship between View
and Activity
? Please refer to the code to make the question clearer.
...
public class MainActivity extends Activity implements OnClickListener {
private Button btn1;
private Button btn2;
@Override
protected void onCreate(Bundle savedInstanceState){
...
btn1 = (Button)findViewById(R.id.button1);
btn2 = (Button)findViewById(R.id.button2);
// Below are the methods calling which confuses me
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
...
}
public void onClick(View v) {
int id = v.get();
switch(id) {
case R.id.button1:
//statement for button1
case R.id.button2:
//statement for button2
...
}
}
}
EDIT:This question is misleading, just caused by overlooking 'setOnClickListener(...)' is the same as 'onClick(...)'. I just don't know how to close this question.