Android アプリを作成する際に、複数の選択肢から1つだけを選択させたいときにラジオボタンは使用されます。
iOSでは、ラジオボタンの定義がないので、セグメントコントロールが該当します。

Android では、ラジオボタンで対応します。
Android を覚えたての際に、「やさしい Android プログラミング」(市販本)を使用して作成すると、こんな感じのラジオボタン画面ができます。
そのときのソース(メイン部分)
2 | tv = new TextView( this ); |
6 | rb1 = new RadioButton( this ); |
7 | rb2 = new RadioButton( this ); |
14 | rg = new RadioGroup( this ); |
やさしいマニュアルのためか?簡単にできてしまった感がありますが、横に並べて使いたいためいろいろ調べると、こんなソースだけではNGであることが判明。
改良版
ラジオボタンを横に並べる方法です。
javaとXMLを使用しています。

Javaソースでは、ラジオボタングループの構成はXMLで定義しています。
2 | TextView TX1Title = (TextView) findViewById(R.id.X1title); |
3 | TX1Title.setText( "ラジオボタン" ); |
6 | TextView TX1rb21 = (TextView) findViewById(R.id.X1rb21); |
7 | TextView TX1rb22 = (TextView) findViewById(R.id.X1rb22); |
14 | TX1rb21.setTextSize( 28 ); |
15 | TX1rb22.setTextSize( 28 ); |
18 | TX1rb21.setText( "設定1" ); |
19 | TX1rb22.setText( "設定2" ); |
22 | RadioGroup gX1rg2 = (RadioGroup) findViewById(R.id.X1rg2); |
23 | gX1rg2.check(R.id.X1rb21); |
2 | android:layout_width= "match_parent" |
3 | android:layout_height= "wrap_content" |
4 | android:textAppearance= "?android:attr/textAppearanceLarge" |
6 | android:id= "@+id/X1title" |
7 | android:layout_gravity= "center_horizontal" |
8 | android:background= "#f6ae32" /> |
11 | android:orientation= "vertical" |
12 | android:layout_width= "match_parent" |
13 | android:layout_height= "wrap_content" |
14 | android:layout_gravity= "center_horizontal" ></LinearLayout> |
17 | android:orientation= "horizontal" |
18 | android:layout_width= "match_parent" |
19 | android:layout_height= "wrap_content" |
20 | android:background= "#e1dada" > |
23 | android:layout_width= "wrap_content" |
24 | android:layout_height= "wrap_content" |
25 | android:layout_gravity= "center_horizontal" |
26 | android:orientation= "horizontal" |
27 | android:id= "@+id/X1rg2" > |
30 | android:layout_width= "wrap_content" |
31 | android:layout_height= "wrap_content" |
32 | android:textAppearance= |
33 | "?android:attr/textAppearanceSmall" |
35 | android:id= "@+id/textView3" /> |
38 | android:layout_width= "wrap_content" |
39 | android:layout_height= "wrap_content" |
41 | android:id= "@+id/X1rb21" |
42 | android:checked= "false" |
43 | android:textColor= "#fa0fcc0f" |
44 | android:textStyle= "bold" /> |
47 | android:layout_width= "wrap_content" |
48 | android:layout_height= "wrap_content" |
50 | android:id= "@+id/X1rb22" |
51 | android:checked= "false" |
52 | android:textColor= "#fa0fcc0f" |
53 | android:textStyle= "bold" /> |
XML上で水平に並べる設定と、radioボタングループを定義しています。
やさしい・・・本は、XMLの概念を消していて簡単な紹介だけになっている点が難点です。
本だけでradioボタンを横に並べるのが、不可能と理解するにはかなり時間が掛かりました。
「これってすごい」「みんなに教えたい!」と思ったらぜひ共有してみてください