2017-04-14 10 views
2

私はクイズアプリを開発中です。前の質問と次の質問の2つのボタンがあります。私がしたいのは、ユーザーがオプションから回答を選択して(オプションのラジオボタンが4つあります)、次の質問を試みるときです。以前にユーザーが選択した同じラジオボタンを有効にする必要があります。そのために、私は選択されたラジオボタンIDを保存するための共有設定を使用しました。 次のボタンクリックの場合: はここに私のコードですアンドロイドスタジオの共有設定で保存されたラジオボタンIDに応じてラジオグループの特定のラジオボタンを有効にする方法

nextButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      int radioSelected = radioGroup.getCheckedRadioButtonId(); 
      int userSelection = getSelectedAnswer(radioSelected); 

      sharedPreferences=getSharedPreferences(MyPREFERENCES,MODE_PRIVATE); 
      SharedPreferences.Editor editor=sharedPreferences.edit(); 
      editor.putInt("Ans",userSelection); 
      editor.commit(); 
      int correctAnswerForQuestion =firstQuestion.getCorrectAnswer(); 
      if(userSelection == correctAnswerForQuestion) 
      { 
       Toast.makeText(getApplicationContext(),"Correct Answer",Toast.LENGTH_LONG).show(); 
       currentQuizQuestion++; 

       if(currentQuizQuestion>=quizCount) 
       { 
        Toast.makeText(getApplicationContext(),"End of quiz",Toast.LENGTH_LONG).show(); 
        return; 
       } 
       else 
       { 
        firstQuestion=parsedObject.get(currentQuizQuestion); 
        txtquestion.setText(firstQuestion.getQuestion()); 
        String[] possibleAnswers=firstQuestion.getAnswers().split(","); 
        uncheckedRadioButton(); 
        optionOne.setText(possibleAnswers[0]); 
        optionTwo.setText(possibleAnswers[1]); 
        optionThree.setText(possibleAnswers[2]); 
        optionFour.setText(possibleAnswers[3]); 
       } 
      } 
      else 
      { 
       Toast.makeText(getApplicationContext(),"You choose wrong answer",Toast.LENGTH_LONG).show(); 
       currentQuizQuestion++; 
       firstQuestion=parsedObject.get(currentQuizQuestion); 
       txtquestion.setText(firstQuestion.getQuestion()); 
       String[] possibleAnswers=firstQuestion.getAnswers().split(","); 
       uncheckedRadioButton(); 
       optionOne.setText(possibleAnswers[0]); 
       optionTwo.setText(possibleAnswers[1]); 
       optionThree.setText(possibleAnswers[2]); 
       optionFour.setText(possibleAnswers[3]); 
      } 
     } 
    }); 

と前のボタンのために:

previousButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 

      currentQuizQuestion--; 
      if(currentQuizQuestion<0) 
      { 
       return; 
      } 
      checkedRadioButton(); 
      firstQuestion=parsedObject.get(currentQuizQuestion); 
      txtquestion.setText(firstQuestion.getQuestion()); 
      String[] possibleAnswers=firstQuestion.getAnswers().split(","); 
      optionOne.setText(possibleAnswers[0]); 
      optionTwo.setText(possibleAnswers[1]); 
      optionThree.setText(possibleAnswers[2]); 
      optionFour.setText(possibleAnswers[3]); 

     } 
    }); 
    JSON_DATA_WEB_CALL(); 

} 
private void checkedRadioButton() { 
    int ans=1; 
    int ans1=sharedPreferences.getInt("Ans",ans); 



    if(optionOne.getId()==ans1) 
    { 

     optionOne.setChecked(true); 


    } 
} 

は私を助けてください。どうもありがとう。

答えて

1

各ラジオボタンオプションoptionOne.setTag( "answer_tag"、 "one")にタグを追加します。 選択したタグを保存し、radiobuttons.getTag( "answer_tag")と格納されたタグを比較します。

関連する問題