Aqlier/ 5月 9, 2017/ Android

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

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


//タイトル表示
tv = new TextView(this);
tv.setText("ラジオボタン");

/radioボタン生成
rb1 = new RadioButton(this);
rb2 = new RadioButton(this);

//ラジオボタンの名称設定
rb1.setText("選択1");
rb2.setText("選択2");

//radioボタンのグループ生成
rg = new RadioGroup(this);

//radioボタンのグループの構成セット
rg.addView(rb1);
rg.addView(rb2);

//radioボタンrb1を選択状態にする
rb1.setChecked(true);

//子view追加
ll.addView(tv);
ll.addView(rg);

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


改良版

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

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


//タイトル
TextView TX1Title = (TextView) findViewById(R.id.X1title);
TX1Title.setText("ラジオボタン");

//ラジオボタン
TextView TX1rb21 = (TextView) findViewById(R.id.X1rb21);
TextView TX1rb22 = (TextView) findViewById(R.id.X1rb22);

//radioボタン高さ設定
TX1rb21.setHeight(50);
TX1rb22.setHeight(50);

//radioボタンフォント設定
TX1rb21.setTextSize(28);
TX1rb22.setTextSize(28);

//radioボタンの名称設定
TX1rb21.setText("設定1");
TX1rb22.setText("設定2");

//ラジオボタングループ
RadioGroup gX1rg2 = (RadioGroup) findViewById(R.id.X1rg2);
gX1rg2.check(R.id.X1rb21);//チェック

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text=""
        android:id="@+id/X1title"
        android:layout_gravity="center_horizontal"
        android:background="#f6ae32" />

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"></LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#e1dada">

        <RadioGroup
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:orientation="horizontal"
            android:id="@+id/X1rg2">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance=
"?android:attr/textAppearanceSmall"
                android:text="  "
                android:id="@+id/textView3" />

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text=""
                android:id="@+id/X1rb21"
                android:checked="false"
                android:textColor="#fa0fcc0f"
                android:textStyle="bold" />

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text=""
                android:id="@+id/X1rb22"
                android:checked="false"
                android:textColor="#fa0fcc0f"
                android:textStyle="bold" />
        </RadioGroup>
    </LinearLayout>

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

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