您好我正在制作某种测验应用程序,我想确保用户至少检查1个复选框。
我正在创建如下的复选框:
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++)
{
final CheckBox box = new CheckBox(this);
box.setText(question[i].answers[zed].answer);
box.setId(zed);
mLinearLayout.addView(box);
int flag = 0;
if(box.isChecked() == true)
{
flag = 1;
}
if(flag == 1)
{
Toast.makeText(getApplicationContext(), "hi", Toast.LENGTH_SHORT).show();
}
}
}
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();
}
但是我无法弄清楚如何确保用户至少检查了一个盒子。
答案 0 :(得分:1)
也许你可以尝试这种方法?
private boolean checkSelectionExists() {
boolean selectionExists = false;
selectionExists = (boolean) ((CheckBox) findViewById(R.id.main_checkbox1)).isChecked() ? true : false ||
(boolean) ((CheckBox) findViewById(R.id.main_checkbox2)).isChecked() ? true : false ||
(boolean) ((CheckBox) findViewById(R.id.main_checkbox3)).isChecked() ? true : false;
return selectionExists;
}
答案 1 :(得分:0)
只需制作一个标志并检查每次,如果复选框为true,则使其为“1”,最后检查是否为“1” 像
这样的东西Box[0]=name_of_first_Checkbox;
Box[1]=name_of_second_Checkbox;
// and so on..
int flag = 0;
for (int i = 0; i < x ; i++){
if(Box[i].isChecked())
{
flag =1;
}
}
if(flag==1){
//At least 1 box is checked
}
答案 2 :(得分:0)
在创建列表时将复选框添加到列表中。
然后使用checkbox.isChecked()
循环中的例程,以确保至少检查1个。
在此处查看复选框API
http://developer.android.com/reference/android/widget/CheckBox.html
以及列表:
http://developer.android.com/reference/java/util/List.html \
// Added List
List<Checkbox> ansList = new List<Checkbox>();
// YOUR CODE with the list addition
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++){
final CheckBox box = new CheckBox(this);
box.setText(question[i].answers[zed].answer);
box.setId(zed);
// Since a test, make sure no answer is checked
box.setChecked(false);
// Add the checkbox to YOUR list of boxes
ansList.add(box);
mLinearLayout.addView(box);
}
}
}
// Later you can check with this
boolean atLeastOne = false;
for (int i = 0; i < question.length ; i++){
if(question[i].multichoice == true){
for(int zed = 0; zed < question[i].answers.length; zed++){
if(ansList.get(zed).isChecked()) atLeastOne = true;
}
}
}
if(true == atLeastOne){
// Do whatever you want to do if at least one is checked
}
我实际上没有运行或检查过这段代码,但是这应该让你开始,它应该可以工作。
答案 3 :(得分:0)
我在阅读你的问题之后给出了一个快速的逻辑,
isCheckButtonClicked