我正在寻找隐藏CheckBox的更好方法。每种情况下都有minium 2和最多5个CheckBox(5个CheckBox和5个CheckBox)。
我用我的数据库的值动态填充答案Arraylist。然后,如果列表有值,我会检查每个案例。如果有值,则CheckBox获取answertext,否则文本为null。之后,我想隐藏所有答案复选框的空值。另外我必须处理相关的解决方案CheckBoxes。这是我实施的方式:
if (question.getQuestiontype() == 2) {
cb_answer1.setText(answerList.size() >= 1 ? answerList.get(0).getAnswertext() : null);
cb_answer2.setText(answerList.size() >= 2 ? answerList.get(1).getAnswertext() : null);
cb_answer3.setText(answerList.size() >= 3 ? answerList.get(2).getAnswertext() : null);
cb_answer4.setText(answerList.size() >= 4 ? answerList.get(3).getAnswertext() : null);
cb_answer5.setText(answerList.size() >= 5 ? answerList.get(4).getAnswertext() : null);
if (cb_answer3.getText().toString().isEmpty()) {
cb_answer3.setVisibility(View.INVISIBLE);
cb_answer4.setVisibility(View.INVISIBLE);
cb_answer5.setVisibility(View.INVISIBLE);
cb_solution3.setVisibility(View.INVISIBLE);
cb_solution4.setVisibility(View.INVISIBLE);
cb_solution5.setVisibility(View.INVISIBLE);
} else {
cb_answer3.setVisibility(View.VISIBLE);
cb_answer4.setVisibility(View.VISIBLE);
cb_answer5.setVisibility(View.VISIBLE);
cb_solution3.setVisibility(View.VISIBLE);
cb_solution4.setVisibility(View.VISIBLE);
cb_solution5.setVisibility(View.VISIBLE);
}
if (cb_answer4.getText().toString().isEmpty()) {
cb_answer4.setVisibility(View.INVISIBLE);
cb_answer5.setVisibility(View.INVISIBLE);
cb_solution4.setVisibility(View.INVISIBLE);
cb_solution5.setVisibility(View.INVISIBLE);
} else {
cb_answer4.setVisibility(View.VISIBLE);
cb_answer5.setVisibility(View.VISIBLE);
cb_solution4.setVisibility(View.VISIBLE);
cb_solution5.setVisibility(View.VISIBLE);
}
if (cb_answer5.getText().toString().isEmpty()) {
cb_answer5.setVisibility(View.INVISIBLE);
cb_solution5.setVisibility(View.INVISIBLE);
} else {
cb_answer5.setVisibility(View.VISIBLE);
cb_solution5.setVisibility(View.VISIBLE);
}
for (CheckBox cb : answerCheckBoxes) {
cb.setEnabled(true);
}
}
看起来有点尴尬,我认为这不是解决这个问题的最佳方法。 你能给我一些建议吗?
答案 0 :(得分:1)
一组视图怎么样?
示例:
CheckBox[] answers = new CheckBox[] {cb_answer1, cb_answer2, cb_answer3, cb_answer4, cb_answer5};
for (int i = answerList.size(); i < answers.length; i++) {
answers[i].setVisibility(View.INVISIBLE);
}
随意扩展!
答案 1 :(得分:1)
首先,如果我理解得很好,我认为你可以像这里一样简化你的代码选择cbs逻辑:
如果(question.getQuestiontype()== 2)
{
cb_answer1.setText(answerList.size() >= 1 ? answerList.get(0).getAnswertext() : null);
cb_answer2.setText(answerList.size() >= 2 ? answerList.get(1).getAnswertext() : null);
cb_answer3.setText(answerList.size() >= 3 ? answerList.get(2).getAnswertext() : null);
cb_answer4.setText(answerList.size() >= 4 ? answerList.get(3).getAnswertext() : null);
cb_answer5.setText(answerList.size() >= 5 ? answerList.get(4).getAnswertext() : null);
if (cb_answer3.getText().toString().isEmpty()) {
cb_answer3.setVisibility(View.INVISIBLE);
cb_solution3.setVisibility(View.INVISIBLE);
} else {
cb_answer3.setVisibility(View.VISIBLE);
cb_solution3.setVisibility(View.VISIBLE);
}
if (cb_answer4.getText().toString().isEmpty()) {
cb_answer4.setVisibility(View.INVISIBLE);
cb_solution4.setVisibility(View.INVISIBLE);
} else {
cb_answer4.setVisibility(View.VISIBLE);
cb_solution4.setVisibility(View.VISIBLE);
}
if (cb_answer5.getText().toString().isEmpty()) {
cb_answer5.setVisibility(View.INVISIBLE);
cb_solution5.setVisibility(View.INVISIBLE);
} else {
cb_answer5.setVisibility(View.VISIBLE);
cb_solution5.setVisibility(View.VISIBLE);
}
for (CheckBox cb : answerCheckBoxes) {
cb.setEnabled(true);
}
}
然后查看此代码,您可以看到您在variuos answer和response cbs上执行相同的操作。所以你可以创建一个这样的参数化方法:
if(question.getQuestiontype()== 2){
cb_answer1.setText(answerList.size() >= 1 ? answerList.get(0).getAnswertext() : null);
cb_answer2.setText(answerList.size() >= 2 ? answerList.get(1).getAnswertext() : null);
cb_answer3.setText(answerList.size() >= 3 ? answerList.get(2).getAnswertext() : null);
cb_answer4.setText(answerList.size() >= 4 ? answerList.get(3).getAnswertext() : null);
cb_answer5.setText(answerList.size() >= 5 ? answerList.get(4).getAnswertext() : null);
this.setCheckboxVisibility(cb_answer3, cb_solution3);
this.setCheckboxVisibility(cb_answer4, cb_solution4);
this.setCheckboxVisibility(cb_answer5, cb_solution5);
for (CheckBox cb : answerCheckBoxes) {
cb.setEnabled(true);
}
}
private void setCheckboxVisibility(Checkbox answer, Checkbox solution) {
if (answer.getText().toString().isEmpty()) {
answer.setVisibility(View.INVISIBLE);
solution.setVisibility(View.INVISIBLE);
} else {
answer.setVisibility(View.VISIBLE);
solution.setVisibility(View.VISIBLE);
}
}
然后,您可以将此解决方案与TWL发布的解决方案结合起来