如何保持单选按钮的选定值?

How to hold selected value of radio button?

我正在做测验应用程序。我有一组 5 个问题,每个问题包含 4 个选项(单选组)以及上一个和下一个按钮。我需要的是当用户 select 回答下一个问题并再次回到上一个问题时,用户 select 之前的答案应该处于 selected 状态。请任何人都可以帮助我我是 android..

的新手
public class MainActivity extends AppCompatActivity {
         List<Questions> quesList;
         int score=0;
         int qid;
        Questions currentQ;
        TextView tv;
        RadioButton rb1,rb2,rb3,rb4;
        ImageButton next,back;
        RadioGroup grp;
        Questions cur;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            DbHelper db=new DbHelper(this);
             grp=(RadioGroup) findViewById(R.id.radiogroup1);
            quesList=db.getAllQuestions();
            if(quesList!= null && quesList.size() !=0) {
                currentQ=quesList.get(qid);
            }

            tv=(TextView) findViewById(R.id.tv1);
            rb1=(RadioButton) findViewById(R.id.radio1);
            rb2=(RadioButton) findViewById(R.id.radio2);
            rb3=(RadioButton) findViewById(R.id.radio3);
            rb4=(RadioButton) findViewById(R.id.radio4);

            next=(ImageButton) findViewById(R.id.forward);
            back=(ImageButton) findViewById(R.id.backward);
            setQuestionView();
            next.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {


                    RadioButton answer=(RadioButton) findViewById(grp.getCheckedRadioButtonId());
                    if(currentQ.getAnswer().equals(answer.getText()))
                   {
                        score++;
                        Log.d("score", "Your score" + score);
                    }

                    if(qid<4){

                        qid++;
                      currentQ=quesList.get(qid);

                        grp.clearCheck();
                        setQuestionView();



                    }else{
                        Intent intent = new Intent(MainActivity.this, ResultActivity.class);
                        Bundle b = new Bundle();
                        b.putInt("score", score); //Your score
                        intent.putExtras(b); //Put your score to your next Intent
                        startActivity(intent);
                        finish();
                    }

                }
            });

            back.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(qid>0){
                        qid--;
                        currentQ=quesList.get(qid);
                        setQuestionView();


                    }
                }
            });
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.menu_main, menu);
            return true;
        }
        private void setQuestionView()
        {
            tv.setText(currentQ.getQuestion());
            rb1.setText(currentQ.getOption1());
            rb2.setText(currentQ.getOption2());
            rb3.setText(currentQ.getOption3());
            rb4.setText(currentQ.getOption4());


        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();

            //noinspection SimplifiableIfStatement
            if (id == R.id.action_settings) {
                return true;
            }
            return super.onOptionsItemSelected(item);
        }

    }

当你给出答案然后得到收音机 按钮 id 并保存到 Arraylist 中, 您当前在哪个列表中显示 问题。

例如: singleton.globalList.get(Constants.GlobalPostion).setAnswer_type(""+buttonID);

并将 if 条件放入 oncreate 方法,检查您是否给出了问题,如果是,则从列表中获取值并设置该 ID 为真.

例如:

    if (isSolve())
    {
          buttonID=**get id which you have been saved in arraylist**.........getAnswer_type());
         RadioButton btn = (RadioButton) rg.getChildAt(buttonID);
         btn.setChecked(true);
    }

希望对您有所帮助。

尝试这样的事情

private void save(int radioid,final boolean isChecked) {

SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("check"+radioid, isChecked);
editor.commit();
}

并获取值

private void load() { 
SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
for(int i=0;i<radioGroup.getChildCount();i++){
   RadioButton rbtn=(RadioButton)radioGroup.getChildAt(i);
   rbtn.setChecked(sharedPreferences.getBoolean("check"+rbtn.getId(), false)); 
 }
}

您可以定义一个 arraylist 来存储答案并最初分配 0s 并且当用户为任何给定问题选择答案时 answers[qid] = //answer 1,2,3,4 最后修改你的 setQuestionView()

private void setQuestionView()
    {
        tv.setText(currentQ.getQuestion());
        rb1.setText(currentQ.getOption1());
        rb2.setText(currentQ.getOption2());
        rb3.setText(currentQ.getOption3());
        rb4.setText(currentQ.getOption4());
        switch(answers[qid]){
            case 1:
              rb1.setChecked(true);
              break;
            case 2:
              rb2.setChecked(true);
              break;
            ...........
            default:
              break;
        }

    }

P.S 我不推荐上述方法,而是建议 re-implementation/factoring 你的代码涉及这种行为。