dimanche 25 janvier 2015

multiple checkboxes. only one is selected. how to get its value

i have this code:



CheckBox aud = (CheckBox)findViewById(R.id.aud);
CheckBox cad = (CheckBox)findViewById(R.id.cad);
CheckBox usd = (CheckBox)findViewById(R.id.usd);
CheckBox gbp = (CheckBox)findViewById(R.id.gbp);
CheckBox eur = (CheckBox)findViewById(R.id.eur);


and this:



int m = 0;
if (aud.isChecked()){
m+= 1;

}
if (cad.isChecked()){
m+= 1;

}
if (usd.isChecked()){
m+= 1;

}
if (gbp.isChecked()){
m+= 1;

}
if (eur.isChecked()){
m+= 1;

}
if (m > 1){
aud.setChecked(false);
gbp.setChecked(false);
usd.setChecked(false);
cad.setChecked(false);
eur.setChecked(false);
m = 0;
Toast.makeText(this, "Please select only one currency", Toast.LENGTH_SHORT).show();
}if (m == 0){
Toast.makeText(this, "Please select a currency", Toast.LENGTH_SHORT).show();
}else{
this.currency = //add the value of the selected checkbox


I need to add the value of the only selected checkbox to the currecny variable. I managed to check if there is only one selected, but i cannot figure out how to get the value of the selected one and place it in a variable called currency which is a class attribute.





Aucun commentaire:

Enregistrer un commentaire