Android的ClearCheck()对RadioGroup有误吗?

10 android android-emulator

我在使用RadioGroup的clearChecked()时遇到了问题.我正在向用户显示一个多项选择题,在用户选择答案后,我会检查答案,给他一些反馈,然后转到下一个问题.在转到下一个问题的过程中,我清楚检查RadioGroup.

任何人都可以向我解释为什么onCheckedChanged方法被调用3次?一旦实际发生了变化(用户更改),一旦我清除检查(使用-1作为选定的ID)和一次之间(用户再次更改)?

据我所知,第二次触发是由clearCheck引起的.代码如下:

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
}
Run Code Online (Sandbox Code Playgroud)

并且run方法看起来像这样

public void run() {
        questionNumber++;
        changeToQuestion(questionNumber);
    }
Run Code Online (Sandbox Code Playgroud)

Ric*_*ler 25

我发现的是,如果一个项目被检查并且你打电话clearCheck()给无线电组,它将调用onCheckedChanged两次.第一次使用已检查项目的ID,第二次使用-1/ View.NO_ID.恕我直言,这是一个错误,显然它已经存在至少1.6.请参阅此Google代码错误报告:http://code.google.com/p/android/issues/detail?id = 4785

似乎唯一的解决方案是检查实际情况RadioButton.isChecked()并测试它是真还是假.这种方式违背了onCheckedChanged返回项目ID 的目的,因为您现在必须保留对这些按钮的引用或findViewById每次调用.

我怀疑他们会解决这个问题,因为改变它可能会以意想不到的方式破坏现有代码.


Swe*_*r ツ 15

我遇到了同样的问题,我解决了其他问题.

  1. 将CheckedChangeListener设置为NULL
  2. 做你的手术
  3. 再次重置OnCheckedChangeListener

代码snnipet:

rdbGroup.setOnCheckedChangeListener(null);
rdbGroup.clearCheck();
rdbGroup.setOnCheckedChangeListener(checkedChangeListener);
Run Code Online (Sandbox Code Playgroud)

希望这会有助于ツ