ラジオボタン (RadioButton) で選択されたボタンの取得
ラジオグループの中で選択されているラジオボタンのオブジェクトを取得するには、
- RadioGroup クラスの getCheckedRadioButtonId を使用し、選択されているラジオボタンの ID を取得する
- findViewById メソッドを使用して、オブジェクトを取得する
という手順となります。
import android.app.Activity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
public class RadioButtonSampleActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// ラジオグループのオブジェクトを取得
RadioGroup rg = (RadioGroup)findViewById(R.id.radiogroup);
// チェックされているラジオボタンの ID を取得
int id = rg.getCheckedRadioButtonId();
// チェックされているラジオボタンオブジェクトを取得
RadioButton radioButton = (RadioButton)findViewById(id);
}
}
ラジオボタン (RadioButton) 関連の説明
⇒ ラジオボタン (RadioButton) の使い方
⇒ 選択状態変更イベント