使用Radiogroup for RadioButton,它们位于单独的TableRow中

时间:2016-01-21 15:36:40

标签: android android-layout android-xml

我需要为多项选择题创建一个ui。例如,在屏幕上将有10个问题,每个问题有4个选项。 对于给定问题,用户只能从可用的4个选项中选择一个选项。

问题是因为我已经为每个选项使用了TableRow。我在4表行中有4个RadioButton。现在我不知道如何使用RadioGroup以便...可以选择给定问题的可用4选项中的一个选项。

下面是示例代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="?attr/actionBarSize"
    android:orientation="vertical"
    android:paddingLeft="20dp"
    android:paddingRight="20dp"
    android:paddingTop="20dp">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            style="@style/rpTvQue"
            android:text="@string/rp_q1" />

        <TableLayout
            android:layout_width="match_parent"
            android:weightSum="10"
            android:layout_height="match_parent">

            <RadioGroup>


            <TableRow>
                <RadioButton
                    android:layout_weight="1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>
                <TextView
                    style="@style/rpTvTblLabelAlt"
                    android:text="@string/rp_q1a" />
            </TableRow>
            <TableRow>
                <RadioButton
                    android:layout_weight="1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>
                <TextView
                    style="@style/rpTvTblLabelAlt"
                    android:text="@string/rp_q1a" />
            </TableRow>
            </RadioGroup>
        </TableLayout>


    </LinearLayout>


</LinearLayout>

屏幕截图:enter image description here 风格:rpTvQue

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="rpTvQue" parent="@android:style/TextAppearance">
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">wrap_content</item>
    </style>
</resources>

样式:rpTvQue

<resources>
    <style name="rpTvTblLabelAlt" parent="@android:style/TextAppearance">
        <item name="android:layout_weight">9</item>
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:paddingLeft">3dp</item>
        <item name="android:paddingRight">3dp</item>
        <item name="android:layout_gravity">fill</item>
    </style>
</resources>

3 个答案:

答案 0 :(得分:1)

RadioGroup是一个LinearLayout,因此您可以为其指定垂直布局,这样您就不需要使用TableLayout和TableRow。另外RadioButton是一个Button,它是一个TextView,具有文本属性,重力和对齐,可用于文本显示,而不是像你一样使用另一个TextView。

答案 1 :(得分:1)

更多关于@AkiAki007所说的你应该在没有TableLayout的情况下使用RadioGroup和RadioButtons

一种方法,不使用适配器:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:padding="16dp"
    android:layout_height="match_parent">

    <!-- Question One Layout -->
    <LinearLayout
        android:id="@+id/question_one_layout"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/textViewQuestion"
            android:text="What you would like to ask for Question one?"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <RadioGroup
            android:id="@+id/radio_group_one"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <RadioButton
                android:id="@+id/radio_button_one_group_one"
                android:text="Answer One"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <RadioButton
                android:id="@+id/radio_button_two_group_one"
                android:text="Answer Two"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <RadioButton
                android:id="@+id/radio_button_three_group_one"
                android:text="Answer Three"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <RadioButton
                android:id="@+id/radio_button_four_group_one"
                android:text="Answer Four"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

        </RadioGroup>
    </LinearLayout>

    <!-- Question One Layout -->
    <LinearLayout
        android:id="@+id/question_two_layout"
        android:layout_below="@+id/question_one_layout"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/textViewQuestionTwo"
            android:text="What you would like to ask for Question two?"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <RadioGroup
            android:id="@+id/radio_group_two"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <RadioButton
                android:id="@+id/radio_button_one_group_two"
                android:text="Answer One"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <RadioButton
                android:id="@+id/radio_button_two_group_two"
                android:text="Answer Two"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <RadioButton
                android:id="@+id/radio_button_three_group_two"
                android:text="Answer Three"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <RadioButton
                android:id="@+id/radio_button_four_group_two"
                android:text="Answer Four"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

        </RadioGroup>
    </LinearLayout>

    <!-- ... And So on for all your questions -->

</RelativeLayout>

更推荐的方法是使用适配器并将其传递给您的问题列表:

import android.content.Context;
import android.os.Parcel;
import android.os.Parcelable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RadioButton;
import android.widget.TextView;

import com.ticketmaster.ticketjoy.R;

import java.util.List;

/**
 * Created on 1/21/16.
 */
public class QuestionAdapter extends BaseAdapter {

    private Context context;
    private List<Question> questions;


    static class Holder{
        TextView tvQuestion;
        RadioButton mRbAnswerOne;
        RadioButton mRbAnswerTwo;
        RadioButton mRbAnswerThree;
        RadioButton mRbAnswerFour;
    }

    public QuestionAdapter(Context context, List<Question> questions){
        this.context = context;
        this.questions = questions;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        Holder holder;

        if(convertView == null){
            holder = new Holder();

            View view = LayoutInflater.from(context).inflate(R.layout.demo_item, parent, false);

            holder.tvQuestion = (TextView) view.findViewById(R.id.textViewQuestion);
            holder.mRbAnswerOne = (RadioButton) view.findViewById(R.id.radio_button_one);
            holder.mRbAnswerTwo = (RadioButton) view.findViewById(R.id.radio_button_two);
            holder.mRbAnswerThree = (RadioButton) view.findViewById(R.id.radio_button_three);
            holder.mRbAnswerFour = (RadioButton) view.findViewById(R.id.radio_button_four);

            convertView.setTag(holder);

        }else{
           holder = (Holder) convertView.getTag();
        }

        /*
            Set the fields for the question and asnwers
         */
        final Question question = questions.get(position);

        holder.tvQuestion.setText(question.question);
        holder.mRbAnswerOne.setText(question.answerOne);
        holder.mRbAnswerTwo.setText(question.answerTwo);
        holder.mRbAnswerThree.setText(question.answerThree);
        holder.mRbAnswerFour.setText(question.answerFour);

        return convertView;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Object getItem(int position) {
        return questions.get(position);
    }

    @Override
    public int getCount() {
        return questions.size();
    }

    public class Question implements Parcelable{

        public String question;
        public String answerOne;
        public String answerTwo;
        public String answerThree;
        public String answerFour;

        public Question(){

        }

        public Question(Parcel source){
            question = source.readString();
            answerOne = source.readString();
            answerTwo = source.readString();
            answerThree = source.readString();
            answerFour = source.readString();
        }

        @Override
        public int describeContents() {
            return 0;
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
              // ... Update don't forget to add them to Parcel 
              dest.writeString(question);
              dest.writeString(answerOne);
              dest.writeString(answerTwo);
              dest.writeString(answerThree);
              dest.writeString(answerFour);
        }

        /**
         * Creator for instantiating the a new Question or array of Questions from a Parcel
         */
        public final Parcelable.Creator<Question> CREATOR = new Parcelable.Creator<Question>(){
            @Override
            public Question createFromParcel(Parcel source) {
                return new Question(source);
            }

            @Override
            public Question[] newArray(int size) {
                return new Question[size];
            }
        };
    }
}

使用布局:

<?xml version="1.0" encoding="utf-8"?>
<!-- Question One Layout -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/question_one_layout"
    android:padding="16dp"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/textViewQuestion"
        android:text="What you would like to ask for Question one?"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <RadioGroup
        android:id="@+id/radio_group"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <RadioButton
            android:id="@+id/radio_button_one"
            android:text="Answer One"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <RadioButton
            android:id="@+id/radio_button_two"
            android:text="Answer Two"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <RadioButton
            android:id="@+id/radio_button_three"
            android:text="Answer Three"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <RadioButton
            android:id="@+id/radio_button_four"
            android:text="Answer Four"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </RadioGroup>
</LinearLayout>

答案 2 :(得分:0)

您可以在每个TableRow中使用单个RadiGroup和4个RadioButtons。