ButtonAndroid Activity クラスで、 XML ファイルへのブリッジと、クリック リスナーの設定が使用されているのを見ました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;
}
}
}
しかし、RadioGroup(別のアクティビティ クラスで) を参照する場合は、 の場合の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);2 番目のスニペットでは宣言されていないのはなぜですか。
それは関連するものですか、RadioGroupそれとも他のオブジェクトにも適用できますか?