Changing multiple radio button text each time when the button is clicked: android

时间:2017-06-15 10:29:36

标签: android

I am making a quiz app in which there are four radio buttons. I want to change the all the four options every time "NEXT" button is clicked. Should I use 2D array or use any other logic. Please give proper logic through coding.

Layout file

Java code

2 个答案:

答案 0 :(得分:0)

您可以使用Butterknife

执行此操作
@BindViews({ R.id.radio1, R.id.radio2, R.id.radio3})
List<RadioButton> radioList;


static final ButterKnife.Action<RadioButton> action = new ButterKnife.Action<RadioButton>() {
  @Override public void apply(RadioButton rb, int index) {
    //Do your stuff here
  }
};


void onNextButton() {
 ButterKnife.apply(radioList, action);
}

答案 1 :(得分:0)

试试这个模型

<强>布局

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

<RadioGroup
    android:id="@+id/radioGroupAnswrs"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true" >

    <RadioButton
        android:id="@+id/radio0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="ansOpt1" />

    <RadioButton
        android:id="@+id/radio1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ansOpt2" />

    <RadioButton
        android:id="@+id/radio2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ansOpt3" />

    <RadioButton
        android:id="@+id/radio3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ansOpt4" />

</RadioGroup>

<Button
    android:id="@+id/btnNext"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:text="Next" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/radioGroupAnswrs"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="30dp"
    android:text="Questin"
    android:textAppearance="?android:attr/textAppearanceMedium" />

 </RelativeLayout>

<强> String.xml `

<string name="app_name">app name</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>

<string-array name="qtns">
    <item >questian1</item>
    <item >questian2</item>
    <item >questian3</item>
    <item >questian4</item>
    <item >questian5</item>
</string-array>

<string-array name="ans">
    <item >#Q1opt1#Q1opt2#Q1opt3#Q1opt4</item>
    <item >#Q2opt1#Q2opt2#Q2opt3#Q2opt4</item>
    <item >#Q3opt1#Q3opt2#Q3opt3#Q3opt4</item>
    <item >#Q4opt1#Q4opt2#Q4opt3#Q4opt4</item>
    <item >#Q5opt1#Q5opt2#Q5opt3#Q5opt4</item>
</string-array>

POJO课程

public class Quiz {

private String question;
private String anwrs[];

public Quiz(String quest, String[] answrs) {
    this.question = quest;
    this.anwrs = answrs;
}

public String getQuestion(){
    return this.question;
}

public String[] getAnsOptions(){
    return this.anwrs;
}

}

活动类

public class QuizActivity extends Activity {

TextView tvQstn;
RadioButton rbOp1, rbOp2, rbOp3, rbOp4;
Button btnNext;

ArrayList<Quiz> aryQuiz;
int quesCnt = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.aa_test);

    aryQuiz = new ArrayList<Quiz>();

    initViews();

    setArray();

    setQuestion();

    btnNext.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            setQuestion();
        }
    });

}

private void initViews() {
    tvQstn = (TextView) findViewById(R.id.textView1);
    rbOp1 = (RadioButton) findViewById(R.id.radio0);
    rbOp2 = (RadioButton) findViewById(R.id.radio1);
    rbOp3 = (RadioButton) findViewById(R.id.radio2);
    rbOp4 = (RadioButton) findViewById(R.id.radio3);
    btnNext = (Button) findViewById(R.id.btnNext);

}

private void setQuestion() {
    if (aryQuiz.size() > quesCnt) {
        Quiz cQuiz = aryQuiz.get(quesCnt);
        tvQstn.setText(cQuiz.getQuestion());
        rbOp1.setText(cQuiz.getAnsOptions()[0]);
        rbOp2.setText(cQuiz.getAnsOptions()[1]);
        rbOp3.setText(cQuiz.getAnsOptions()[2]);
        rbOp4.setText(cQuiz.getAnsOptions()[3]);
        quesCnt++;
    }
}

private void setArray() {

    String[] qtns = getResources().getStringArray(R.array.qtns);
    String[] ans = getResources().getStringArray(R.array.ans);

    for (int i = 0; i < qtns.length; i++) {
        Quiz quiz = new Quiz(qtns[i], ans[i].split("#"));
        aryQuiz.add(quiz);
    }

}

}