选中多个复选框时执行操作
Perform an action when a number of checkboxes are checked
我的应用程序有 10 个复选框,选中 5 个复选框后我想在下面的 ImageView 中显示图片。但我不知道如何编码。如果这里有人可以帮助我使用此代码,我将非常感激。
您可以为所有复选框添加checkedListener,并为复选框初始化一个计数器。
检查更新计数器以检查每个复选框,当计数器达到计数 5 时执行您需要的操作
checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked) {
checkBoxCounter = checkboxCounter + 1;
if(checkBoxCounter == 5)
//do your imageview task here;
imageView.setImageResource(R.drawable.ic_image);
} else {
//if checkbox is unchecked
checkBoxCounter = checkBoxCounter - 1;
}
}
});
将此代码放入所有 onCheckedListener 复选框中
public class babyconquistas extends AppCompatActivity {
EditText conqtext1, conqtext2,
Button button;
ImageView img, img1;
CheckBox conq1, conq2, conq3, conq4, conq5, conq6, conq7, conq8, conq9, conq10;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_babyconquistas);
img = (ImageView) findViewById(R.id.imgconq2);
img1 = (ImageView) findViewById(R.id.imgconq4);
conq1 = (CheckBox) findViewById(R.id.conq1);
conq2 = (CheckBox) findViewById(R.id.conq2);
conq3 = (CheckBox) findViewById(R.id.conq3);
conq4 = (CheckBox) findViewById(R.id.conq4);
conq5 = (CheckBox) findViewById(R.id.conq5);
conq6 = (CheckBox) findViewById(R.id.conq6);
conq7 = (CheckBox) findViewById(R.id.conq7);
conq8 = (CheckBox) findViewById(R.id.conq8);
conq9 = (CheckBox) findViewById(R.id.conq9);
conq10 = (CheckBox) findViewById(R.id.conq10);
conqtext1 = (EditText) findViewById(R.id.conq1text);
conqtext2 = (EditText) findViewById(R.id.conq2text);
button = (Button) findViewById(R.id.salvar);
loadSavedPreferences();
}
//checkboxes
private void loadSavedPreferences() {
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(this);
boolean cbValue1 = sp.getBoolean("CHECKBO1", false);
boolean cbValue2 = sp.getBoolean("CHECKBO2", false);
boolean cbValue3 = sp.getBoolean("CHECKBO3", false);
boolean cbValue4 = sp.getBoolean("CHECKBO4", false);
boolean cbValue5 = sp.getBoolean("CHECKBO5", false);
boolean cbValue6 = sp.getBoolean("CHECKBO6", false);
boolean cbValue7 = sp.getBoolean("CHECKBO7", false);
boolean cbValue8 = sp.getBoolean("CHECKBO8", false);
boolean cbValue9 = sp.getBoolean("CHECKBO9", false);
boolean cbValue10 = sp.getBoolean("CHECKBO10", false);
if (cbValue1) {
conq1.setChecked(true);
} else {
conq1.setChecked(false);
}
if (cbValue2) {
conq2.setChecked(true);
} else {
conq2.setChecked(false);
}
if (cbValue3) {
conq3.setChecked(true);
} else {
conq3.setChecked(false);
}
if (cbValue4) {
conq4.setChecked(true);
} else {
conq4.setChecked(false);
}
if (cbValue5) {
conq5.setChecked(true);
} else {
conq5.setChecked(false);
}
if (cbValue6) {
conq6.setChecked(true);
} else {
conq6.setChecked(false);
}
if (cbValue7) {
conq7.setChecked(true);
} else {
conq7.setChecked(false);
}
if (cbValue8) {
conq8.setChecked(true);
} else {
conq8.setChecked(false);
}
if (cbValue9) {
conq9.setChecked(true);
} else {
conq9.setChecked(false);
}
if (cbValue10) {
conq10.setChecked(true);
} else {
conq10.setChecked(false);
}
}
private void savePreferences(String key, boolean value) {
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(this);
SharedPreferences.Editor edit = sp.edit();
edit.putBoolean(key, value);
edit.commit();
}
private void savePreferences(String key, String value) {
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(this);
SharedPreferences.Editor edit = sp.edit();
edit.putString(key, value);
edit.commit();
}
public void Salvar(View v) {
// TODO Auto-generated method stub
savePreferences("N1", conqtext1.getText().toString());
savePreferences("N2", conqtext2.getText().toString());
savePreferences("CHECKBO1", conq1.isChecked());
savePreferences("CHECKBO2", conq2.isChecked());
savePreferences("CHECKBO3", conq3.isChecked());
savePreferences("CHECKBO4", conq4.isChecked());
savePreferences("CHECKBO5", conq5.isChecked());
savePreferences("CHECKBO6", conq6.isChecked());
savePreferences("CHECKBO7", conq7.isChecked());
savePreferences("CHECKBO8", conq8.isChecked());
savePreferences("CHECKBO9", conq9.isChecked());
savePreferences("CHECKBO10", conq10.isChecked());
}
有 10 个复选框。
如果第一组 5 个复选框全部选中,则显示 ImageView img
还有第二组5个checkbox,如果这5个checkbox都被选中,显示ImageViw img1
我希望你能理解,我真的很感谢你花时间帮助我,真的非常感谢你,如果更简单,当然如果你想要,我有 teamViewer。
public class babyconquistas extends AppCompatActivity {
EditText conqtext1, conqtext2,
Button button;
ImageView img, img1;
CheckBox conq1, conq2, conq3, conq4, conq5, conq6, conq7, conq8, conq9, conq10;
int cbCounter1=0, cbCounter2=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_babyconquistas);
img = (ImageView) findViewById(R.id.imgconq2);
img1 = (ImageView) findViewById(R.id.imgconq4);
conq1 = (CheckBox) findViewById(R.id.conq1);
conq2 = (CheckBox) findViewById(R.id.conq2);
conq3 = (CheckBox) findViewById(R.id.conq3);
conq4 = (CheckBox) findViewById(R.id.conq4);
conq5 = (CheckBox) findViewById(R.id.conq5);
conq6 = (CheckBox) findViewById(R.id.conq6);
conq7 = (CheckBox) findViewById(R.id.conq7);
conq8 = (CheckBox) findViewById(R.id.conq8);
conq9 = (CheckBox) findViewById(R.id.conq9);
conq10 = (CheckBox) findViewById(R.id.conq10);
conqtext1 = (EditText) findViewById(R.id.conq1text);
conqtext2 = (EditText) findViewById(R.id.conq2text);
button = (Button) findViewById(R.id.salvar);
conq1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
cbCounter1 = cbCounter1 + 1;
} else {
cbCounter1 = cbCounter1 - 1;
}
if(cbCounter1 == 5) {
img.setImageResource(R.drawable.img);
//or if you want to handle it with visibility
img.setVisibility(View.VISIBLE);
}
}
});
conq2.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
cbCounter1 = cbCounter1 + 1;
} else {
cbCounter1 = cbCounter1 - 1;
}
if(cbCounter1 == 5) {
img.setImageResource(R.drawable.img);
//or if you want to handle it with visibility
img.setVisibility(View.VISIBLE);
}
}
});
conq3.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
cbCounter1 = cbCounter1 + 1;
} else {
cbCounter1 = cbCounter1 - 1;
}
if(cbCounter1 == 5) {
img.setImageResource(R.drawable.img);
//or if you want to handle it with visibility
img.setVisibility(View.VISIBLE);
}
}
});
conq4.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
cbCounter1 = cbCounter1 + 1;
} else {
cbCounter1 = cbCounter1 - 1;
}
if(cbCounter1 == 5) {
img.setImageResource(R.drawable.img);
//or if you want to handle it with visibility
img.setVisibility(View.VISIBLE);
}
}
});
conq5.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
cbCounter1 = cbCounter1 + 1;
} else {
cbCounter1 = cbCounter1 - 1;
}
if(cbCounter1 == 5) {
img.setImageResource(R.drawable.img);
//or if you want to handle it with visibility
img.setVisibility(View.VISIBLE);
}
}
});
conq6.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
cbCounter2 = cbCounter2 + 1;
} else {
cbCounter2 = cbCounter2 - 1;
}
if(cbCounter2 == 5) {
img1.setImageResource(R.drawable.img);
//or if you want to handle it with visibility
img1.setVisibility(View.VISIBLE);
}
}
});
conq7.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
cbCounter2 = cbCounter2 + 1;
} else {
cbCounter2 = cbCounter2 - 1;
}
if(cbCounter2 == 5) {
img1.setImageResource(R.drawable.img);
//or if you want to handle it with visibility
img1.setVisibility(View.VISIBLE);
}
}
});
conq8.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
cbCounter2 = cbCounter2 + 1;
} else {
cbCounter2 = cbCounter2 - 1;
}
if(cbCounter2 == 5) {
img1.setImageResource(R.drawable.img);
//or if you want to handle it with visibility
img1.setVisibility(View.VISIBLE);
}
}
});
conq9.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
cbCounter2 = cbCounter2 + 1;
} else {
cbCounter2 = cbCounter2 - 1;
}
if(cbCounter2 == 5) {
img1.setImageResource(R.drawable.img);
//or if you want to handle it with visibility
img1.setVisibility(View.VISIBLE);
}
}
});
conq10.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
cbCounter2 = cbCounter2 + 1;
} else {
cbCounter2 = cbCounter2 - 1;
}
if(cbCounter2 == 5) {
img1.setImageResource(R.drawable.img);
//or if you want to handle it with visibility
img1.setVisibility(View.VISIBLE);
}
}
});
}
此代码将完成任务。如果您需要将信息存储在 sharedPref 中,那么您可以存储计数器值和已选中的复选框信息,然后在页面加载时您需要填充已选中的复选框和计数器的所有数据
我的应用程序有 10 个复选框,选中 5 个复选框后我想在下面的 ImageView 中显示图片。但我不知道如何编码。如果这里有人可以帮助我使用此代码,我将非常感激。
您可以为所有复选框添加checkedListener,并为复选框初始化一个计数器。 检查更新计数器以检查每个复选框,当计数器达到计数 5 时执行您需要的操作
checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked) {
checkBoxCounter = checkboxCounter + 1;
if(checkBoxCounter == 5)
//do your imageview task here;
imageView.setImageResource(R.drawable.ic_image);
} else {
//if checkbox is unchecked
checkBoxCounter = checkBoxCounter - 1;
}
}
});
将此代码放入所有 onCheckedListener 复选框中
public class babyconquistas extends AppCompatActivity {
EditText conqtext1, conqtext2,
Button button;
ImageView img, img1;
CheckBox conq1, conq2, conq3, conq4, conq5, conq6, conq7, conq8, conq9, conq10;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_babyconquistas);
img = (ImageView) findViewById(R.id.imgconq2);
img1 = (ImageView) findViewById(R.id.imgconq4);
conq1 = (CheckBox) findViewById(R.id.conq1);
conq2 = (CheckBox) findViewById(R.id.conq2);
conq3 = (CheckBox) findViewById(R.id.conq3);
conq4 = (CheckBox) findViewById(R.id.conq4);
conq5 = (CheckBox) findViewById(R.id.conq5);
conq6 = (CheckBox) findViewById(R.id.conq6);
conq7 = (CheckBox) findViewById(R.id.conq7);
conq8 = (CheckBox) findViewById(R.id.conq8);
conq9 = (CheckBox) findViewById(R.id.conq9);
conq10 = (CheckBox) findViewById(R.id.conq10);
conqtext1 = (EditText) findViewById(R.id.conq1text);
conqtext2 = (EditText) findViewById(R.id.conq2text);
button = (Button) findViewById(R.id.salvar);
loadSavedPreferences();
}
//checkboxes
private void loadSavedPreferences() {
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(this);
boolean cbValue1 = sp.getBoolean("CHECKBO1", false);
boolean cbValue2 = sp.getBoolean("CHECKBO2", false);
boolean cbValue3 = sp.getBoolean("CHECKBO3", false);
boolean cbValue4 = sp.getBoolean("CHECKBO4", false);
boolean cbValue5 = sp.getBoolean("CHECKBO5", false);
boolean cbValue6 = sp.getBoolean("CHECKBO6", false);
boolean cbValue7 = sp.getBoolean("CHECKBO7", false);
boolean cbValue8 = sp.getBoolean("CHECKBO8", false);
boolean cbValue9 = sp.getBoolean("CHECKBO9", false);
boolean cbValue10 = sp.getBoolean("CHECKBO10", false);
if (cbValue1) {
conq1.setChecked(true);
} else {
conq1.setChecked(false);
}
if (cbValue2) {
conq2.setChecked(true);
} else {
conq2.setChecked(false);
}
if (cbValue3) {
conq3.setChecked(true);
} else {
conq3.setChecked(false);
}
if (cbValue4) {
conq4.setChecked(true);
} else {
conq4.setChecked(false);
}
if (cbValue5) {
conq5.setChecked(true);
} else {
conq5.setChecked(false);
}
if (cbValue6) {
conq6.setChecked(true);
} else {
conq6.setChecked(false);
}
if (cbValue7) {
conq7.setChecked(true);
} else {
conq7.setChecked(false);
}
if (cbValue8) {
conq8.setChecked(true);
} else {
conq8.setChecked(false);
}
if (cbValue9) {
conq9.setChecked(true);
} else {
conq9.setChecked(false);
}
if (cbValue10) {
conq10.setChecked(true);
} else {
conq10.setChecked(false);
}
}
private void savePreferences(String key, boolean value) {
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(this);
SharedPreferences.Editor edit = sp.edit();
edit.putBoolean(key, value);
edit.commit();
}
private void savePreferences(String key, String value) {
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(this);
SharedPreferences.Editor edit = sp.edit();
edit.putString(key, value);
edit.commit();
}
public void Salvar(View v) {
// TODO Auto-generated method stub
savePreferences("N1", conqtext1.getText().toString());
savePreferences("N2", conqtext2.getText().toString());
savePreferences("CHECKBO1", conq1.isChecked());
savePreferences("CHECKBO2", conq2.isChecked());
savePreferences("CHECKBO3", conq3.isChecked());
savePreferences("CHECKBO4", conq4.isChecked());
savePreferences("CHECKBO5", conq5.isChecked());
savePreferences("CHECKBO6", conq6.isChecked());
savePreferences("CHECKBO7", conq7.isChecked());
savePreferences("CHECKBO8", conq8.isChecked());
savePreferences("CHECKBO9", conq9.isChecked());
savePreferences("CHECKBO10", conq10.isChecked());
}
有 10 个复选框。
如果第一组 5 个复选框全部选中,则显示 ImageView img
还有第二组5个checkbox,如果这5个checkbox都被选中,显示ImageViw img1
我希望你能理解,我真的很感谢你花时间帮助我,真的非常感谢你,如果更简单,当然如果你想要,我有 teamViewer。
public class babyconquistas extends AppCompatActivity {
EditText conqtext1, conqtext2,
Button button;
ImageView img, img1;
CheckBox conq1, conq2, conq3, conq4, conq5, conq6, conq7, conq8, conq9, conq10;
int cbCounter1=0, cbCounter2=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_babyconquistas);
img = (ImageView) findViewById(R.id.imgconq2);
img1 = (ImageView) findViewById(R.id.imgconq4);
conq1 = (CheckBox) findViewById(R.id.conq1);
conq2 = (CheckBox) findViewById(R.id.conq2);
conq3 = (CheckBox) findViewById(R.id.conq3);
conq4 = (CheckBox) findViewById(R.id.conq4);
conq5 = (CheckBox) findViewById(R.id.conq5);
conq6 = (CheckBox) findViewById(R.id.conq6);
conq7 = (CheckBox) findViewById(R.id.conq7);
conq8 = (CheckBox) findViewById(R.id.conq8);
conq9 = (CheckBox) findViewById(R.id.conq9);
conq10 = (CheckBox) findViewById(R.id.conq10);
conqtext1 = (EditText) findViewById(R.id.conq1text);
conqtext2 = (EditText) findViewById(R.id.conq2text);
button = (Button) findViewById(R.id.salvar);
conq1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
cbCounter1 = cbCounter1 + 1;
} else {
cbCounter1 = cbCounter1 - 1;
}
if(cbCounter1 == 5) {
img.setImageResource(R.drawable.img);
//or if you want to handle it with visibility
img.setVisibility(View.VISIBLE);
}
}
});
conq2.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
cbCounter1 = cbCounter1 + 1;
} else {
cbCounter1 = cbCounter1 - 1;
}
if(cbCounter1 == 5) {
img.setImageResource(R.drawable.img);
//or if you want to handle it with visibility
img.setVisibility(View.VISIBLE);
}
}
});
conq3.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
cbCounter1 = cbCounter1 + 1;
} else {
cbCounter1 = cbCounter1 - 1;
}
if(cbCounter1 == 5) {
img.setImageResource(R.drawable.img);
//or if you want to handle it with visibility
img.setVisibility(View.VISIBLE);
}
}
});
conq4.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
cbCounter1 = cbCounter1 + 1;
} else {
cbCounter1 = cbCounter1 - 1;
}
if(cbCounter1 == 5) {
img.setImageResource(R.drawable.img);
//or if you want to handle it with visibility
img.setVisibility(View.VISIBLE);
}
}
});
conq5.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
cbCounter1 = cbCounter1 + 1;
} else {
cbCounter1 = cbCounter1 - 1;
}
if(cbCounter1 == 5) {
img.setImageResource(R.drawable.img);
//or if you want to handle it with visibility
img.setVisibility(View.VISIBLE);
}
}
});
conq6.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
cbCounter2 = cbCounter2 + 1;
} else {
cbCounter2 = cbCounter2 - 1;
}
if(cbCounter2 == 5) {
img1.setImageResource(R.drawable.img);
//or if you want to handle it with visibility
img1.setVisibility(View.VISIBLE);
}
}
});
conq7.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
cbCounter2 = cbCounter2 + 1;
} else {
cbCounter2 = cbCounter2 - 1;
}
if(cbCounter2 == 5) {
img1.setImageResource(R.drawable.img);
//or if you want to handle it with visibility
img1.setVisibility(View.VISIBLE);
}
}
});
conq8.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
cbCounter2 = cbCounter2 + 1;
} else {
cbCounter2 = cbCounter2 - 1;
}
if(cbCounter2 == 5) {
img1.setImageResource(R.drawable.img);
//or if you want to handle it with visibility
img1.setVisibility(View.VISIBLE);
}
}
});
conq9.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
cbCounter2 = cbCounter2 + 1;
} else {
cbCounter2 = cbCounter2 - 1;
}
if(cbCounter2 == 5) {
img1.setImageResource(R.drawable.img);
//or if you want to handle it with visibility
img1.setVisibility(View.VISIBLE);
}
}
});
conq10.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
cbCounter2 = cbCounter2 + 1;
} else {
cbCounter2 = cbCounter2 - 1;
}
if(cbCounter2 == 5) {
img1.setImageResource(R.drawable.img);
//or if you want to handle it with visibility
img1.setVisibility(View.VISIBLE);
}
}
});
}
此代码将完成任务。如果您需要将信息存储在 sharedPref 中,那么您可以存储计数器值和已选中的复选框信息,然后在页面加载时您需要填充已选中的复选框和计数器的所有数据