【问题标题】:Use RadioButtons within one activity在一项活动中使用 RadioButtons
【发布时间】:2017-05-29 18:41:55
【问题描述】:

我正在制作一个测验应用程序,其中有一些问题:


当用户点击NEXT QUESTION 时,将显示另一个问题。 我已经制作了当前代码:

public void onRadioButtonClicked(View view) {
    // Is the button now checked?
    boolean checked = ((RadioButton) view).isChecked();

    // Check which radio button was clicked
    switch(view.getId()) {
        case R.id.number_1:
            if (checked)
                point = 0;
            Toast.makeText(this, ""+ point,
                    Toast.LENGTH_LONG).show();

            break;
        case R.id.number_2:
            if (checked)
                point = 1;
            Toast.makeText(this, ""+ point,
                    Toast.LENGTH_LONG).show();

            break;
        case R.id.number_3:
            if (checked)
                point = 2;
            Toast.makeText(this, ""+ point,
                    Toast.LENGTH_LONG).show();

            break;
        case R.id.number_4:
            if (checked)
                point = 3;
            Toast.makeText(this, ""+ point,
                    Toast.LENGTH_LONG).show();

            break;
    }
}

当单击NEXT QUESTION 时,如何在同一活动中弹出另一个问题时发生这种情况?

【问题讨论】:

  • 您只需要在文本视图中添加新内容并清除下一个问题的所有单选按钮
  • 我建议你使用viewpager,你可以有多个页面。

标签: android radio-button android-radiobutton


【解决方案1】:

1.如果你使用RadioGroup,那么当NextQestion按钮首先被点击时clear的状态RadioButton调用方法RadioGroup.clearCheck()

2. 从您的database 或其他地方获取下一个question,并将值设置为您的TextViewRadioButtons

这是一个例子:

public class Main2Activity extends AppCompatActivity {

    TextView textQuestion;
    RadioGroup radioGroup;
    RadioButton radioButton1,radioButton2,radioButton3;
    Button buttonNextQuestion;

    int questionNo = 1;

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

        textQuestion = (TextView) findViewById(R.id.text_question); 
        radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
        radioButton1 = (RadioButton) findViewById(R.id.radioButton1);
        radioButton2 = (RadioButton) findViewById(R.id.radioButton2);
        radioButton3 = (RadioButton) findViewById(R.id.radioButton3);
        buttonNextQuestion = (Button) findViewById(R.id.button_next_question);

        buttonNextQuestion.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                questionNo++;
                loadNextQuestion();
            }
        });
    }

    public void loadNextQuestion() {

        String question, option1, option2, option3, option4;

        // Get question and options from database or from other place
        // and store into >> question, option1, option2, option3, option4

        // Update UI
        // Clear radio button state
        radioGroup.clearCheck();

        // Set new question and its options to view
        textQuestion.setText(question);
        radioButton1.setText(option1);
        radioButton2.setText(option2);
        radioButton3.setText(option3);
        radioButton4.setText(option4);
    }
}

【讨论】:

  • 感谢您接受我的回答。如果我的回答似乎有用,那么您可以投票。在此先感谢:)
【解决方案2】:

你有你的 questionTextView,只需使用 questionTextView.setText(newQuestion)

将您的所有单选按钮设置为 false/未选中 idk 是正确的。


但是最好在你的活动中放置一个 FrameLayout 并在你的用户每次回答问题并构建一个新 View 时调用一个新的 QuestionFragment ,你的控制器将更加独立。

【讨论】:

    【解决方案3】:

    你可以这样做

    public void nextQuestion(View view){
    
       questionTextView.setText(gtString(R.string.nextQuestion));
       checkBox1.setText("option1");
       checkBox2.setText("option2");
       checkBox3.setText("option3");
       checkBox4.setText("option4");
    }
    

    将以上所有视图名称替换为您的

    【讨论】:

      猜你喜欢
      • 2015-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多