In an android Activity class, I saw the bridging to a Button
of XML file and the setting it for click listener was using findViewById()
:
public class MyClass1 extends Activity implements OnClickListener {
Button b;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.photo);
b = (Button) findViewById(R.id.button1); //This is where I have question
b.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
break;
case R.id.button2:
break;
}
}
}
But As for referencing a RadioGroup
(In another Activity class), It wasn't pointed to the Object by the findViewById()
as it was for the Button
:
public class MyClass2 extends Activity implements OnCheckedChangeListener {
RadioGroup rg;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.photo);
//Why isn't this here? --> rg = (RadioGroup) findViewById(R.id.radiogroup);
rg.setOnCheckedChangeListener(this);
}
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
switch (checkedId) {
case R.id.rBad:
break;
case R.id.rGood:
break;
}
}
}
I mean in both onClick()
and onCheckedChanged()
methods the id of objects is referenced to.
So why b = (Button) findViewById(R.id.button1);
is declared in the first code snippt, while the rg = (RadioGroup) findViewById(R.id.radiogroup);
isn't in the second snippet.
Is it something related to RadioGroup
or it's applicable to other objects too?