【问题标题】:How to hold selected value of radio button?如何保持单选按钮的选定值?
【发布时间】:2015-10-13 14:06:17
【问题描述】:

我正在做测验应用程序。我有 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);
        }

    }

【问题讨论】:

    标签: android radio-button


    【解决方案1】:

    您可以定义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 我不推荐上述方法,而是建议重新实现/分解您的代码以涉及此行为。

    【讨论】:

    • 并记录分数。并确保您最终不会因为一个正确的问题而获得多分。
    【解决方案2】:

    试试这样的

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

    【讨论】:

      【解决方案3】:

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

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

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

      例如:

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

      希望这会对你有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-04-03
        • 2011-03-26
        • 2021-04-14
        • 1970-01-01
        • 2013-09-06
        • 2014-06-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多