问题描述
例如,我有5个复选框(CB1 - CB5)。 CB1优于其他的,这意味着,如果所有的其他4检查它只能进行检查。如果我检查CB1,其他4会自动得到遏制。这是我当前的Java code(这code是onCreate方法内):
For example, I have 5 checkboxes (cb1 - cb5). cb1 is superior to others, which means it can only be checked if all the other 4 are checked. And if I check cb1, all the other 4 should automatically get checked. This is my current java code (this code is within onCreate method):
final CheckBox cb1 = (CheckBox) findViewById(R.id.checkBox1);
final CheckBox cb2 = (CheckBox) findViewById(R.id.checkBox2);
final CheckBox cb3 = (CheckBox) findViewById(R.id.checkBox3);
final CheckBox cb4 = (CheckBox) findViewById(R.id.checkBox4);
final CheckBox cb5 = (CheckBox) findViewById(R.id.checkBox5);
cb1.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( cb1.isChecked() )
{
cb2.isChecked();
cb3.isChecked();
cb4.isChecked();
cb5.isChecked();
}
}
});
我是初学者所以code可能是完全错误的,或有只是一个小错误,我无法看到它。无论哪种方式,我AP preciate的帮助。另外,如果我的方式太复杂,有一个更简单的方法,让我知道吧。
I'm a complete beginner so the code may be completely wrong or there's just a small error and I can't see it. Either way, I'd appreciate the help. Also, if my way is too complicated and there's an easier way, let me know please.
推荐答案
有关复选框监听器创建一个类
Create a class for checkbox listener
public class CheckBoxListener implements OnCheckedChangeListener {
public CheckBoxListener() {
}
@Override
public void onCheckedChanged(CompoundButton view, boolean isChecked) {
switch(view.getId()) {
case R.id.checkBox1:
//do something
case R.id.checkBox2:
case ...
}
}
}
那么这个监听器设置为所有的复选框:
Then set this listener to all your checkboxes:
final CheckBox cb1 = (CheckBox) findViewById(R.id.checkBox1);
final CheckBox cb2 = (CheckBox) findViewById(R.id.checkBox2);
final CheckBox cb3 = (CheckBox) findViewById(R.id.checkBox3);
final CheckBox cb4 = (CheckBox) findViewById(R.id.checkBox4);
final CheckBox cb5 = (CheckBox) findViewById(R.id.checkBox5);
cb1.setOnCheckedChangeListener(new CheckBoxListener());
cb2.setOnCheckedChangeListener(new CheckBoxListener());
cb3.setOnCheckedChangeListener(new CheckBoxListener());
cb4.setOnCheckedChangeListener(new CheckBoxListener());
这篇关于如何让我的复选框做一些事情时,它的检查?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!