我在一个RadioGroups
中有两个layout
,每个radiogroup
有三个radio buttons
。那么如何使用if else statement
。
[每个组中有两个radiogroups
有三个radiobuttons
。当用户点击一个单选按钮时,它会显示正确的答案。错误答案。] [1]
答案 0 :(得分:1)
为什么要使用if else
语句?你可以轻松地完成它。
int radioButtonId = rbGroup.getCheckedRadioButtonId();
View radioButton = radioButtonGroup.findViewById(radioButtonId);
答案 1 :(得分:0)
您可以这样做以从radioGroup
获取已检查的radiobuttonRadioGroup g = (RadioGroup) findViewById(R.id.rBtnDigits);
switch (g.getCheckedRadioButtonId())
{
case R.id.rbtnButton1
//do something
break;
case R.id.rbtnButton2
//do something
break;
}
答案 2 :(得分:-2)
rg = (RadioGroup) findViewById(R.id.radioGroup1);
button = (RadioButton) findViewById(R.id.radio0);
rg.performClick();
rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
@SuppressWarnings("unused")
int selected = rg.getCheckedRadioButtonId();
pos = rg.indexOfChild(findViewById(rg.getCheckedRadioButtonId()));
switch (pos) {
case 0:
Toast.makeText(getBaseContext(),
"You have Clicked RadioButton 1",
Toast.LENGTH_SHORT).show();
break;
case 1:
Toast.makeText(getBaseContext(),
"You have Clicked RadioButton 2",
Toast.LENGTH_SHORT).show();
break;
case 2:
Toast.makeText(getBaseContext(),
"You have Clicked RadioButton 3",
Toast.LENGTH_SHORT).show();
break;
default:
Toast.makeText(getBaseContext(),
"You have Clicked RadioButton 4",
Toast.LENGTH_SHORT).show();
break;
}
}
});