java - 检查是否选中了所有复选框。安卓
问题描述
我正在尝试处理一个事件,如果该人选中所有复选框,则应该发生一个动作,即使按钮可点击。
到目前为止,我只能检查一个框是选中还是未选中,但找不到一种方法来设置一个计数器来计算单击了多少个复选框。
复选框将是动态的,但在此代码示例中,我只使用了静态值。
我试图设置一个计数器,如果我单击一个复选框,它应该增加但无法找出正确的方法。
// 我调用的方法
ArrayList<String> arrayList= new ArrayList<>();
arrayList.add("2Pac");
arrayList.add("Biggie");
arrayList.add("Nas");
// Create Checkbox Dynamically
for(int i=0;i<arrayList.size();i++) {
CheckBox checkBox = new CheckBox(getContext());
checkBox.setText(arrayList.get(i));
checkBox.setTextSize(20);
checkBox.setId(++checkBoxID);
checkBox.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
// int counter = -1;
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
// String msg = "You have " + (isChecked ? "checked" : "unchecked") + " this box it Checkbox.";
//Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
if(checkBox.isChecked())
{
//Toast.makeText(getActivity(), ""+ counter, Toast.LENGTH_SHORT).show();
}else
{
// counter++;
// Toast.makeText(getActivity(), ""+ counter, Toast.LENGTH_SHORT).show();
}
}
});
for(int j=0;j<allCheckBox.size();j++)
{
if(allCheckBox.size()==arrayList.size())
{
//Toast.makeText(MainActivity.this, ""+ checkBox.getId(), Toast.LENGTH_SHORT).show();
}
}
// Add Checkbox to LinearLayout
if (linearLayout != null) {
linearLayout.addView(checkBox);
}
到目前为止,我已经花了一天多的时间试图弄清楚这个!任何帮助或想法都非常感谢。
解决方案
在 for 循环外定义计数器,初始值为 0,当用户选中复选框时,将其加一,当用户取消选中复选框时,将计数器减一,并在同一个onCheckedChanged侦听器中检查复选框的数量,如下所示
ArrayList<String> arrayList= new ArrayList<>();
arrayList.add("2Pac");
arrayList.add("Biggie");
arrayList.add("Nas");
int counter = 0; //all checkboxes unchecked
// Create Checkbox Dynamically
for(int i=0;i<arrayList.size();i++) {
CheckBox checkBox = new CheckBox(getContext());
checkBox.setText(arrayList.get(i));
checkBox.setTextSize(20);
checkBox.setId(++checkBoxID);
checkBox.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if(checkBox.isChecked())
{
counter++;
if (counter == arrayList.size()) {
//make your button clickable here
}
} else {
counter--;
//disable your button incase the user uncheck one of the checkboxes after all checkboxes were checked
} //end if
}
});
// Add Checkbox to LinearLayout
if (linearLayout != null) {
linearLayout.addView(checkBox);
} //end if
} //end for
我用手机写了代码,所以我没有测试代码我希望它能工作
推荐阅读
- javascript - Textarea 点击字数统计 | 改变 | keuyup 以及通过单击按钮填充文本区域时
- llvm-clang - 错误:'.section' 指令 .section .multiboot 中出现意外标记
- php - Laravel 从父类别及其所有子类别中获取所有产品
- command-line - 如何将 pg_restore 恢复日志附加到文件
- python - Python SQLite3 [] 和 () 的区别
- python - tkinter:无法在图像标签上将文本向左对齐
- rust - Rust 包括 mod
- grails - 我怎样才能得到一个内容
- ruby-on-rails - 外键列上的 Rails 迁移更改约束 - 解决方法?
- python-3.x - mac os x python 不同版本卸载