【问题标题】:Getting text from a radio button that is part of a radio group从属于单选组的单选按钮获取文本
【发布时间】:2017-02-15 22:32:11
【问题描述】:

这是我在课堂上使用的两种方法,我希望返回附加到每个单选按钮的文本字符串:

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;
    }

然后我有了这个,它应该利用这些方法并将字符串返回给变量 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()”将返回一个字符序列而不是一个字符串。相反,我只是将“selectedToppingRad.getText()”插入到 putExtra 中,它就起作用了。

【问题讨论】:

    标签: android-studio switch-statement radio-group android-radiogroup android-radiobutton


    【解决方案1】:

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

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

    为什么不只是获取 id 然后获取它的文本?即使在同一组中,他们也有唯一的 ID。 就像@Turuu 那里的评论一样;而是吐司,把它放在字符串中

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

    反正我只是个样本

    【讨论】:

    • 感谢您的回答。是的,我看过那个,但我可能想多了。
    猜你喜欢
    • 2012-11-08
    • 2013-12-06
    • 2011-05-16
    • 1970-01-01
    • 2018-11-25
    • 1970-01-01
    • 1970-01-01
    • 2020-06-11
    • 1970-01-01
    相关资源
    最近更新 更多