多个复选框。只有一个被选中。如何获得它的价值
multiple checkboxes. only one is selected. how to get its value
我有这个代码:
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);
还有这个:
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
我需要将唯一选中的复选框的值添加到 currecny 变量。我设法检查是否只选择了一个,但我无法弄清楚如何获取所选值并将其放入一个名为 currency 的变量中,该变量是 class 属性。
尝试使用RadioGroup
RadioGroup rd= (RadioGroup)findViewById(R.id.radioGroup1);
rd.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
public void onCheckedChanged(RadioGroup group, int checkedId) {
// checkedId is the RadioButton selected
RadioButton rb=(RadioButton)findViewById(checkedId);
Toast.makeText(getApplicationContext(), rb.getText(), Toast.LENGTH_SHORT).show();
// rb.getText() = to get Value
}
});
xml :
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="180dp"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="CAD" />
<RadioButton
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="EUR" />
<RadioButton
android:id="@+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TUN" />
</RadioGroup>
我有这个代码:
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);
还有这个:
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
我需要将唯一选中的复选框的值添加到 currecny 变量。我设法检查是否只选择了一个,但我无法弄清楚如何获取所选值并将其放入一个名为 currency 的变量中,该变量是 class 属性。
尝试使用RadioGroup
RadioGroup rd= (RadioGroup)findViewById(R.id.radioGroup1);
rd.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
public void onCheckedChanged(RadioGroup group, int checkedId) {
// checkedId is the RadioButton selected
RadioButton rb=(RadioButton)findViewById(checkedId);
Toast.makeText(getApplicationContext(), rb.getText(), Toast.LENGTH_SHORT).show();
// rb.getText() = to get Value
}
});
xml :
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="180dp"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="CAD" />
<RadioButton
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="EUR" />
<RadioButton
android:id="@+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TUN" />
</RadioGroup>