質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Q&A

0回答

969閲覧

RadioButtonの選択を1つにしたい

Ragito19

総合スコア14

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

0グッド

0クリップ

投稿2021/11/20 12:13

RadioButtonの選択を1つにしたい

ここに質問の内容を詳しく書いてください。
RadioButtonの項目を4つにしました。
横4つに並べるとレイアウトの見栄えが悪くなるため、縦×横
2×2にして分けました。分けるのに使ったのはTableRowです。


RadioGroupを使っても、2つ以上選択すると、
下のような状態になってしまいます。
イメージ説明

下が自分が作ったソースコードです。xmlファイルとjavaファイルが2つあります。

<activity.main2.xml>

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical">

<ScrollView android:id="@+id/scrollView" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:paddingTop="50dp" android:paddingRight="10dp" android:paddingBottom="50dp" android:text="@string/game" android:textSize="30dp" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginBottom="20dp" android:text="@string/question1" android:textSize="16sp" /> <RadioGroup android:id="@+id/Rg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true"> <TableRow android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="40dp"> <RadioButton android:id="@+id/radioButton4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="30dp" android:text="モンスト" /> <RadioButton android:id="@+id/radioButton5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="黒ウィズ" /> </TableRow> <TableRow android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="40dp" android:layout_marginBottom="50dp"> <RadioButton android:id="@+id/radioButton6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="30dp" android:text="パズドラ" /> <RadioButton android:id="@+id/radioButton7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="ピクミンBloom" /> </TableRow> </RadioGroup> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginBottom="20dp" android:text="@string/question2" android:textSize="16sp" /> <CheckBox android:id="@+id/checkBox" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:text="遊びやすい" /> <CheckBox android:id="@+id/checkBox2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:text="ストーリーが魅力的" /> <CheckBox android:id="@+id/checkBox4" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:text="面白い" /> <CheckBox android:id="@+id/checkBox5" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:text="独特な世界観がある" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginTop="50dp" android:layout_marginBottom="50dp" android:text="@string/question3" android:textSize="16sp" /> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginBottom="50dp" android:hint="コラボを増やして欲しい。"> </EditText> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="center" android:layout_marginLeft="150dp" android:layout_marginRight="150dp" android:backgroundTint="#FF9800" android:onClick="button_onClick2" android:text="結果を送信" /> </LinearLayout> </ScrollView>
</RelativeLayout>

SubActivity.java

package com.example.questionnaire;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioGroup;

public class SubActivity extends AppCompatActivity {

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); RadioGroup group = (RadioGroup)findViewById(R.id.Rg); group.check(R.id.Rg); } public void button_onClick2(View view) { Intent i = new Intent(this, SubActivity2.class); startActivity(i); }

}

試したこと

横上で4つ並べてそれをRadioGroupで囲った時は、1つのみに選択できました。
しかし、4つのRadioButtonの項目をRadioGroupで囲ってみたのですが、1つに選択されませんでした。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問