【问题标题】:How Handle If all RadioGroup Checked?如果检查了所有 RadioGroup 如何处理?
【发布时间】:2019-03-17 20:22:48
【问题描述】:

我有 60 个项目的 RecyclerView。那个项目有 RadioGroup 并且在这个有两个单选按钮

我也使用 DataBinding

我有 FabButton,如果单击此按钮,我将转到结果活动,但如果选中所有 RadioGroup :)

我写了这样的代码,它可以工作,但有一些错误......当我点击第 60 项时,即使不检查 59 项,我将转到结果活动。

为什么?

这是我的代码:

//Select All Radio Group
public boolean allSelected() {
    boolean allChecked = true;
    for (Question question : questions) {
        allChecked = question.getSelectedId() != 0;
    }
    return allChecked;
}

//Card background if Uncheck the question
private void showHideErrorBackground(boolean show) {
    for (Question question : questions) {
        question.setShowErrorBackground(show);
    }
    mbtiQuestAdapter.notifyDataSetChanged();
}

我喜欢这个方法:

if (allSelected()) {

  Intent intent = new Intent(MbtiQuestionActivity.this, ResultActivity.class);

        startActivity(intent);

} else {
        snack bar =
          Snackbar.make(coordinator, R.string.check_all_ques_err, Snackbar.LENGTH_SHORT)

            .setAction(R.string.snack_find_unchecked_ques, new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                showHideErrorBackground(true);
              }
            });

        ViewCompat.setLayoutDirection(snackbar.getView(), ViewCompat.LAYOUT_DIRECTION_RTL);
        snackbar.show();
}

Question.java(模型数据)

public class Question extends BaseQuestion {

  private int selectedId;
  private boolean showErrorBackground;

  @Bindable
  public void setShowErrorBackground(boolean showErrorBackground) {
    this.showErrorBackground = showErrorBackground;
    notifyPropertyChanged(BR.showErrorBackground);
  }

  @Bindable
  public int getSelectedId() {
    return selectedId;
  }

  public void setSelectedId(int selectedId) {
    this.selectedId = selectedId;
    notifyPropertyChanged(BR.selectedId);
  }

  public boolean isShowErrorBackground() {
    return showErrorBackground;
  }
}

谢谢你的帮助

【问题讨论】:

    标签: java android radio-group


    【解决方案1】:

    在您的allSelected 方法中,您直接将变量allChecked 的值设置为循环中当前问题的选中状态。这意味着只要检查最后一个问题,allChecked 的值就会是true。同样,如果不检查,则始终为 false,但这符合您应用的行为,因此您可能不会遇到任何问题。

    相反,您应该使用 AND 操作,如下所示:

    allChecked = allChecked && (question.getSelectedId() != 0);
    

    由于您的for 循环会在任何问题未得到解答时中断,因此您可以通过以下方式优化代码:

    for (Question question : questions) {
          if (question.getSelectedId() == 0) {
              allChecked = false;
              break;
          }
    }
    

    在此代码中,一旦遇到未回答的问题,allChecked 将设置为 false,其余问题将被跳过。您无需每次迭代都使用 AND 操作。

    【讨论】:

    • 是的,你说得对..这个代码对我来说是正确的..非常感谢亲爱的
    • 当然 .. 只需转到代码... 之后我检查它是否接受。谢谢
    【解决方案2】:

    这个方法返回的布尔值就是最后一个question.getSelectedId()的值。

    public boolean allSelected() {
        boolean allChecked = true;
        for (Question question : questions) {
           allChecked = question.getSelectedId() != 0;
        }
        return allChecked;
    }
    

    试试这个:

    public boolean allSelected() {
        for (Question question : questions) {
            if (question.getSelectedId() == 0)
                return false;
        }
        return true;
    }
    

    【讨论】:

    • 是的,这也是正确的 :) 非常感谢
    猜你喜欢
    • 2019-02-20
    • 1970-01-01
    • 1970-01-01
    • 2011-12-06
    • 1970-01-01
    • 2013-11-09
    • 1970-01-01
    • 1970-01-01
    • 2020-04-18
    相关资源
    最近更新 更多