从属于单选组的单选按钮获取文本

Getting text from a radio button that is part of a radio group

这是我在 class 中使用的两种方法,我希望 return 每个单选按钮附带的文本字符串:

public String toppingGroupCheck(RadioGroup toppingGroup, int checkedId){
        switch (checkedId){
            case R.id.toppingRadio1:
                checkedId = toppingGroup.getCheckedRadioButtonId();
                selectedToppingRad = (RadioButton)findViewById(checkedId);
                selectedTopping = selectedToppingRad.getText().toString();
            case R.id.toppingRadio2:
                checkedId = toppingGroup.getCheckedRadioButtonId();
                selectedToppingRad = (RadioButton)findViewById(checkedId);
                selectedTopping = selectedToppingRad.getText().toString();
            case R.id.toppingRadio3:
                checkedId = toppingGroup.getCheckedRadioButtonId();
                selectedToppingRad = (RadioButton)findViewById(checkedId);
                selectedTopping = selectedToppingRad.getText().toString();
            case R.id.toppingRadio4:
                checkedId = toppingGroup.getCheckedRadioButtonId();
                selectedToppingRad = (RadioButton)findViewById(checkedId);
                selectedTopping = selectedToppingRad.getText().toString();
            case R.id.toppingRadio5:
                checkedId = toppingGroup.getCheckedRadioButtonId();
                selectedToppingRad = (RadioButton)findViewById(checkedId);
                selectedTopping = selectedToppingRad.getText().toString();
        }
        return selectedTopping;
    }

    public String sideGroupCheck(RadioGroup sidesGroup, int checkedId){
        switch (checkedId){
            case R.id.sideRadio1:
                checkedId = sidesGroup.getCheckedRadioButtonId();
                selectedSideRad = (RadioButton)findViewById(checkedId);
                selectedSide = selectedSideRad.getText().toString();
            case R.id.sideRadio2:
                checkedId = sidesGroup.getCheckedRadioButtonId();
                selectedSideRad = (RadioButton)findViewById(checkedId);
                selectedSide = selectedSideRad.getText().toString();
            case R.id.sideRadio3:
                checkedId = sidesGroup.getCheckedRadioButtonId();
                selectedSideRad = (RadioButton)findViewById(checkedId);
                selectedSide = selectedSideRad.getText().toString();
        }
        return selectedSide;
    }

然后我有了这个,它应该利用这些方法和 return 字符串到变量 tmpTopping 和 tmpSide:

public void submitForm(View view){
        Intent submitform = new Intent(this, submitForm.class);

        String tmpTopping = toppingGroupCheck(toppingGroup, checkedId);
        submitform.putExtra("topping",tmpTopping);
        String tmpSide = sideGroupCheck(sidesGroup, checkedId);
        submitform.putExtra("side",tmpSide);
        startActivity(submitform);
    }

有人可以解释我可能做错了什么,或者更好的方法来解决他的问题吗?谢谢

更新: 我尝试这样做:

    checkedId = toppingGroup.getCheckedRadioButtonId();
    selectedToppingRad = (RadioButton)findViewById(checkedId);
    String topping = selectedToppingRad.getText();

但它一直说 "selectedToppingRad.getText()" 会 return 一个字符序列而不是一个字符串。相反,我只是将 "selectedToppingRad.getText()" 直接插入 putExtra 并且它起作用了。

我真的不知道你在用你的代码做什么,但我认为这可能会有所帮助

访问 Android getting value from selected radiobutton 并查看答案。

为什么不先获取 ID 然后获取它的文本?他们甚至在同一组中也有唯一的 ID。 喜欢 @Turuu 那里的评论;而是敬酒,把它放在字符串

radioButton = (RadioButton) findViewById(checkedId);
String radioButtonText = radioButton.getText();

反正我也只是一个样本