0
아래에서 알 수 있듯이 저는 여러 유형의 질문 (RadioButtons 및 CheckBoxes)을 사용하여 일종의 퀴즈를 동적으로 생성하고 있습니다.
그래서 나는 그것들을 올바르게 생성 할 수 있습니다.
질문 끝에 정상적인 버튼이 있습니다.
그리고 나는 모든 질문에 답을 얻은 후에 만 클릭 가능하게하고 싶습니다.
모든 RadioButton에 응답 할 수있는 기능이 있습니다.
하지만 두 가지 유형의 버튼이 있으므로 CheckBox 답변의 질문에 적어도 1 개의 확인 된 답변이 있는지 확인하는 방법을 알 수 없습니다. 당신은 예를 들어 (i*10)+zed
에 체크 박스의 ID를 설정 질문 미만 10 개 답변이있을 경우Android 퀴즈 애플리케이션 - 모든 질문에 답변 함을 확인하십시오.
ArrayList<RadioGroup> arrGroup;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
buttHole = (Button) findViewById(R.id.button1);
buttHole.setEnabled(false);
getQuestions();
//Creating the list of Questions
LinearLayout mLinearLayout = (LinearLayout) findViewById(R.id.linear1);
arrGroup = new ArrayList<RadioGroup>();
try
{
for (int i = 0; i <= question.length; i++)
{
if(question[i].multichoice == true)
{
TextView title2 = new TextView(this);
title2.setText(question[i].questionName);
title2.setTextColor(Color.BLACK);
mLinearLayout.addView(title2);
for(int zed = 0; zed < question[i].answers.length; zed++)
{
CheckBox box = new CheckBox(this);
box.setText(question[i].answers[zed].answer);
box.setId(zed);
mLinearLayout.addView(box);
}
}
else
{
// create text button
TextView title = new TextView(this);
title.setText(question[i].questionName);
title.setTextColor(Color.BLACK);
mLinearLayout.addView(title);
// create radio button
final RadioButton[] rb = new RadioButton[question[i].answers.length];
RadioGroup radGr = new RadioGroup(this);
// take a reference of RadioGroup view
arrGroup.add(radGr);
radGr.setOrientation(RadioGroup.VERTICAL);
radGr.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
checkQuestionsAnswer();
}
});
for (int j = 0; j < question[i].answers.length; j++) {
rb[j] = new RadioButton(this);
radGr.addView(rb[j]);
rb[j].setText(question[i].answers[j].answer);
}
mLinearLayout.addView(radGr);
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
public boolean checkQuestionsAnswer()
{
int count = arrGroup.size();
for (int i = 0; i < count; i++)
{
if (arrGroup.get(i).getCheckedRadioButtonId() == -1)
{
Toast.makeText(getApplicationContext(), "Not all questions are answered!", Toast.LENGTH_SHORT).show();
return false;
}
}
Toast.makeText(getApplicationContext(), "Thank you!", Toast.LENGTH_SHORT).show();
buttHole.setEnabled(true);
//Return true if all answer are given
return true;
}
아무 이유없이 작동하지 않았 음 :/확실하지 않은 이유가 – user3182266
어디에서 디버깅 할 수 있습니까? 정확히 무슨 일을하지 않았습니까? –