Android で、ラジオボタンの設定

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

Android では、ラジオボタンで対応します。
Android を覚えたての際に、「やさしい Android プログラミング」(市販本)を使用して作成すると、こんな感じのラジオボタン画面ができます。
そのときのソース(メイン部分)

1//タイトル表示
2tv = new TextView(this);
3tv.setText("ラジオボタン");
4 
5/radioボタン生成
6rb1 = new RadioButton(this);
7rb2 = new RadioButton(this);
8 
9//ラジオボタンの名称設定
10rb1.setText("選択1");
11rb2.setText("選択2");
12 
13//radioボタンのグループ生成
14rg = new RadioGroup(this);
15 
16//radioボタンのグループの構成セット
17rg.addView(rb1);
18rg.addView(rb2);
19 
20//radioボタンrb1を選択状態にする
21rb1.setChecked(true);
22 
23//子view追加
24ll.addView(tv);
25ll.addView(rg);

やさしいマニュアルのためか?簡単にできてしまった感がありますが、横に並べて使いたいためいろいろ調べると、こんなソースだけではNGであることが判明。


改良版

ラジオボタンを横に並べる方法です。
javaとXMLを使用しています。

Javaソースでは、ラジオボタングループの構成はXMLで定義しています。

1//タイトル
2TextView TX1Title = (TextView) findViewById(R.id.X1title);
3TX1Title.setText("ラジオボタン");
4 
5//ラジオボタン
6TextView TX1rb21 = (TextView) findViewById(R.id.X1rb21);
7TextView TX1rb22 = (TextView) findViewById(R.id.X1rb22);
8 
9//radioボタン高さ設定
10TX1rb21.setHeight(50);
11TX1rb22.setHeight(50);
12 
13//radioボタンフォント設定
14TX1rb21.setTextSize(28);
15TX1rb22.setTextSize(28);
16 
17//radioボタンの名称設定
18TX1rb21.setText("設定1");
19TX1rb22.setText("設定2");
20 
21//ラジオボタングループ
22RadioGroup gX1rg2 = (RadioGroup) findViewById(R.id.X1rg2);
23gX1rg2.check(R.id.X1rb21);//チェック
1    <TextView
2        android:layout_width="match_parent"
3        android:layout_height="wrap_content"
4        android:textAppearance="?android:attr/textAppearanceLarge"
5        android:text=""
6        android:id="@+id/X1title"
7        android:layout_gravity="center_horizontal"
8        android:background="#f6ae32" />
9 
10    <LinearLayout
11        android:orientation="vertical"
12        android:layout_width="match_parent"
13        android:layout_height="wrap_content"
14        android:layout_gravity="center_horizontal"></LinearLayout>
15 
16    <LinearLayout
17        android:orientation="horizontal"
18        android:layout_width="match_parent"
19        android:layout_height="wrap_content"
20        android:background="#e1dada">
21 
22        <RadioGroup
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">
28 
29            <TextView
30                android:layout_width="wrap_content"
31                android:layout_height="wrap_content"
32                android:textAppearance=
33"?android:attr/textAppearanceSmall"
34                android:text="  "
35                android:id="@+id/textView3" />
36 
37            <RadioButton
38                android:layout_width="wrap_content"
39                android:layout_height="wrap_content"
40                android:text=""
41                android:id="@+id/X1rb21"
42                android:checked="false"
43                android:textColor="#fa0fcc0f"
44                android:textStyle="bold" />
45 
46            <RadioButton
47                android:layout_width="wrap_content"
48                android:layout_height="wrap_content"
49                android:text=""
50                android:id="@+id/X1rb22"
51                android:checked="false"
52                android:textColor="#fa0fcc0f"
53                android:textStyle="bold" />
54        </RadioGroup>
55    </LinearLayout>

XML上で水平に並べる設定と、radioボタングループを定義しています。

やさしい・・・本は、XMLの概念を消していて簡単な紹介だけになっている点が難点です。
本だけでradioボタンを横に並べるのが、不可能と理解するにはかなり時間が掛かりました。

Verified by MonsterInsights