逆引きAndroid入門 ラジオボタン(RadioButton)を使用するには

ラジオボタン(RadioButton)を使用するには

| トラックバック(0) |

概要

android.widget パッケージ - RadioGroup クラス、
android.widget パッケージ - RadioButton クラスを使用します。

 

ソースコード

RadioGroup radioGroup = (RadioGroup) findViewById(id.radiogroup);
// 指定した ID のラジオボタンをチェックします
radioGroup.check(id.radiobutton_green);
// チェックされているラジオボタンの ID を取得します
RadioButton radioButton = (RadioButton) findViewById(radioGroup.getCheckedRadioButtonId());
// ラジオグループのチェック状態が変更された時に呼び出されるコールバックを登録します
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
    // ラジオグループのチェック状態が変更された時に呼び出されます
    public void onCheckedChanged(
        RadioGroup group,
        int checkedId) { // チェック状態が変更されたラジオボタンの ID が渡されます
        RadioButton radioButton = (RadioButton) findViewById(checkedId);
    }
});

 

リソース

<RadioGroup android:id="@+id/radiogroup"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <RadioButton android:id="@+id/radiobutton_red"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Red" />
    <RadioButton android:id="@+id/radiobutton_green"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Green" />
    <RadioButton android:id="@+id/radiobutton_blue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Blue" />
</RadioGroup>

トラックバック(0)

トラックバックURL: http://mt.adakoda.com/mt-tb.cgi/67

逆引きAndroid入門 ラジオボタン(RadioButton)を使用するには