如何在android中使用if else语句为radiobuttons

时间:2015-06-26 08:50:32

标签: java android

我在一个RadioGroups中有两个layout,每个radiogroup有三个radio buttons。那么如何使用if else statement

[每个组中有两个radiogroups有三个radiobuttons。当用户点击一个单选按钮时,它会显示正确的答案。错误答案。] [1]

3 个答案:

答案 0 :(得分:1)

为什么要使用if else语句?你可以轻松地完成它。

 int radioButtonId = rbGroup.getCheckedRadioButtonId();
 View radioButton = radioButtonGroup.findViewById(radioButtonId);

答案 1 :(得分:0)

您可以这样做以从radioGroup

获取已检查的radiobutton
RadioGroup 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;

            }
        }

    });