在一个 android Activity 类中,我看到了一个ButtonXML 文件的桥接,并且为单击侦听器设置它正在使用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;
}
}
}
但是至于引用 a RadioGroup(在另一个 Activity 类中),它并没有被指向 Object ,findViewById()因为它是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;
}
}
}
我的意思是在onClick()和onCheckedChanged()方法中都引用了对象的 id。
那么为什么b = (Button) findViewById(R.id.button1);在第一个代码片段中声明,而rg = (RadioGroup) findViewById(R.id.radiogroup);在第二个片段中没有。
它是否与其他对象相关RadioGroup或也适用于其他对象?