从作为广播组

时间:2016-10-07 02:38:57

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

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

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

然后我有了这个,它应该利用那些方法并将String返回给变量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中,然后就可以了。

1 个答案:

答案 0 :(得分:1)

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

访问Android getting value from selected radiobutton并寻找答案。

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

ajax: function(opt) {

        var defaultData = settings.getDefaultDataset();
        var self = this;
        var opt = $.extend({}, defaultData, opt);
        var output = [];
        return new Promise(function(resolve, reject) {
            token = window.es.token;
            opt[token] = "1";
            jQuery.ajax({

                method: "POST",
                url: self.system.path+"/index.php",
                "data": opt,
                error: function() {
                    reject('error');
                },  
                success: function(result) {
                    output.push(opt, result);
                    resolve(output);           
                }
            });

        });
    }

我只是一个样本