Error in Android's clearCheck() for RadioGroup

2020-02-28 04:41发布

问题:

I'm having an issue with RadioGroup's clearChecked(). I'm displaying a multiple choice question to the user and after the user selects an answer I check the answer, give him some feedback and then move to the next question. In the process of moving to the next question I clearCheck on the RadioGroup.

Can anyone explain to me why the onCheckedChanged method is called 3 times? Once when the change actually occurs (with the user changes), once when I clearCheck(with -1 as the selected id) and once in between (with the user changes again)?

As far as I could tell the second trigger is provoked by clearCheck. Code below:

private void checkAnswer(RadioGroup group, int checkedId){
    // this makes sure it doesn't blow up when the check is cleared
    // also we don't check the answer when there is no answer
    if (checkedId == -1) return;
    if (group.getCheckedRadioButtonId() == -1) return;

    // check if correct answer
    if (checkedId == validAnswerId){
        score++;
        this.giveFeedBack(feedBackType.GOOD);
    } else {
        this.giveFeedBack(feedBackType.BAD);
    }
    // allow for user to see feedback and move to next question
    h.postDelayed(this, 800);
}

private void changeToQuestion(int questionNumber){
    if (questionNumber >= this.questionSet.size()){
        // means we are past the question set
        // we're going to the score activity
        this.goToScoreActivity();
        return;
    }
    //clearing the check
    gr.clearCheck();
    // give change the feedback back to question
    imgFeedback.setImageResource(R.drawable.question_mark); //OTHER CODE HERE
}

and the run method looks like this

public void run() {
        questionNumber++;
        changeToQuestion(questionNumber);
    }

回答1:

What I've discovered is that if an item is checked and you call clearCheck() on the radio group it will call onCheckedChanged twice. The first time with the id of the item that was checked and the second time with -1/View.NO_ID. IMHO, this is a bug and apparently it has been around since at least 1.6. See this google code bug report: http://code.google.com/p/android/issues/detail?id=4785

It seems to be that the only solution is to check the actual RadioButton.isChecked() and test if it is true or false. This sort of defeats the purpose of the onCheckedChanged returning the id of the item since you now have to either keep references to those buttons or call findViewById every time.

I doubt they will fix this since changing it would probably break existing code in unexpected ways.



回答2:

I faced the same problem and i solved with other work-around.

  1. Set CheckedChangeListener as NULL
  2. Do your operation
  3. Reset your OnCheckedChangeListener again

Code snnipet :

rdbGroup.setOnCheckedChangeListener(null);
rdbGroup.clearCheck();
rdbGroup.setOnCheckedChangeListener(checkedChangeListener);

Hope this will help ツ



回答3:

I had similar problem. My solution:

in procedure:

public void onCheckedChanged(RadioGroup rGroup, int checkedId)

I check checkedId. It equals to -1 if you use clearCheck() else it equals to selected radiogroup child