android.widget.RadioButtonを使用します。
RadioButtonを使ったサンプル
■ソースコード
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.radiobutton_layout);
// ラジオボタンを変更した場合のイベントを設定
((RadioGroup)findViewById(R.id.radiogroup)).setOnCheckedChangeListener(this);
}
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// チェックされたボタンを取得
RadioButton button = (RadioButton)group.findViewById(checkedId);
// チェックされたボタンを確認
Log.d("TAG", "radiobutton:" + button.getText());
}
■レイアウト
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:padding="0dp" >
<RadioGroup
android:id="@+id/radiogroup"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/sample_radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ぼたん1" />
<RadioButton
android:id="@+id/sample_radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ぼたん2" />
<RadioButton
android:id="@+id/sample_radio3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ぼたん3" />
</RadioGroup>
</FrameLayout>
0 件のコメント:
コメントを投稿