我的应用有10个复选框,当选中5个复选框时,我想在下面的ImageView中显示图片。但我不知道如何编码。如果这里有人可以帮我解决这个问题,我会非常感激。
答案 0 :(得分:0)
您可以将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
的所有复选框中答案 1 :(得分:0)
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个复选框,如果全部选中此5个复选框,则显示ImageViw img1
我希望你明白,我真的很感谢你花时间帮助我,非常感谢你,如果更容易,如果你想要的话,我有teamViewer。
答案 2 :(得分:0)
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中,那么您可以存储计数器值并选中,复选框信息,然后在页面加载时,您需要填充已选中复选框和计数器的所有数据