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

时间:2015-10-13 04:28:03

标签: android radio-button

我在做测验应用。我有5个问题,每个问题包含4个选项(无线电组),上一个和下一个按钮。我需要的是当用户选择答案并转到下一个问题并再次回到上一个问题时,之前选择的用户应该处于选择状态..请任何可以帮助我我是新来的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);
        }

    }

3 个答案:

答案 0 :(得分:2)

您可以定义arraylist来存储答案,并最初分配0s,并在用户为任何给定问题answers[qid] = //answer 1,2,3,4选择答案时最后修改您的{ {1}}到

setQuestionView()

P.S我不建议使用上述方法,而是建议重新实现/分解代码以引发此行为。

答案 1 :(得分:0)

当你给出答案然后收到广播  按钮ID并保存到Arraylist中, 你当前用于哪个列表显示 问题

例如: singleton.globalList.get(Constants.GlobalPostion)的 setAnswer_type(&#34;&#34 + 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);
    }

希望这会对你有所帮助。

答案 2 :(得分:0)

尝试这样的事情

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)); 
 }
}