如果点击不同的单选按钮,只会保存最后一个的值

If different radio buttons are clicked, only the value of the last one will be saved

这是我创建的测验应用程序的第 2 部分。第一部分工作正常,但在这一部分,当我单击单选按钮时,分数只会叠加。例如,如果我点击单选按钮 2,然后点击单选按钮 3,分数将变为 3 而不是 2。

如果我尝试将 getAnswer() 移动到按钮中的 OnClickListener,如果我单击一个单选按钮,它将不会继续到下一个 activity,除非我单击另一个单选按钮。

   protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_part2);


   firebaseAuth = FirebaseAuth.getInstance();
   firebaseDatabase = FirebaseDatabase.getInstance();

   gadChoice1 = (RadioButton)findViewById(R.id.part2Choice1);
   gadChoice2 = (RadioButton)findViewById(R.id.part2Choice2);
   gadChoice3 = (RadioButton)findViewById(R.id.part2Choice3);
   gadChoice4 = (RadioButton)findViewById(R.id.part2Choice4);
   btnNext = (Button)findViewById(R.id.btnNext);

   getAnswer();

   DatabaseReference myRef = firebaseDatabase.getReference(firebaseAuth.getUid()).child("test");
   myRef.addValueEventListener(new ValueEventListener() {
       @Override
       public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
           UserHistory userHistory = dataSnapshot.getValue(UserHistory.class);
           anxHistory = userHistory.getAnx();
           depHistory = userHistory.getDep();

       }

       @Override
       public void onCancelled(@NonNull DatabaseError databaseError) {
           Toast.makeText(Part2.this, databaseError.getCode(), Toast.LENGTH_SHORT).show();
       }
   });

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


                   anxValue = anxHistory + anx;

                   final DatabaseReference myRef = firebaseDatabase.getReference(firebaseAuth.getUid()).child("test");

                   myRef.addListenerForSingleValueEvent(new ValueEventListener() {
                       @Override
                       public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                           HashMap<String, Object> result = new HashMap<>();
                           result.put("anx", anxValue);
                           myRef.updateChildren(result);
                       }

                       @Override
                       public void onCancelled(@NonNull DatabaseError databaseError) {
                           System.out.println("The read failed: " + databaseError.getMessage());
                       }
                   });

           if(dep > anx){
               Intent myIntent = new Intent(Part2.this, Part3.class);
               startActivity(myIntent);
               finish();
           }else if (anx > dep){
               Intent myIntent = new Intent(Part2.this, Part3.class);
               startActivity(myIntent);
               finish();
           }
       }
   });   }

   private void getAnswer() {
   radioGroup2 = (RadioGroup) findViewById(R.id.radioGroup2);
   radioGroup2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

       @Override
       public void onCheckedChanged(RadioGroup group, int checkedId) {
           if (checkedId == R.id.part2Choice2) {
               anx++ ;
           } else if (checkedId == R.id.part2Choice3)  {
               anx = anx + 2 ;
           }
           else if (checkedId == R.id.part2Choice4) {
               anx = anx + 3;
           }


       }
   });

   }

我希望输出是所选单选按钮的值,而不是我单击的单选按钮的总和。例如,如果我单击单选按钮 A (0)、单选按钮 B (1)、单选按钮 C(2),我将记录的输出是 2。

您无法在 onCheckedChanged 中处理它,因为每次您更改所选内容时它都会递增。您可以在单击 btnNext 时对所选的进行测试:

int selectedItem = radioGroup2.getCheckedRadioButtonId();
if (selectedItem == R.id.part2Choice2) {
    anx++ ;
} else if (checkedId == R.id.part2Choice3)  {
    anx = anx + 2 ;
}
else if (checkedId == R.id.part2Choice4) {
    anx = anx + 3;
}