2 sachinks SachinKS 于 2013.06.18 13:40 提问

Android-单选按钮的值设定

求助,应用不管选中哪个按钮,价格总是525.00。不知道怎么搞的。

        //Get and set package selected.
        radioPackageGroup = (RadioGroup)   findViewById(R.id.radTadPackage);
        int selectedId = radioPackageGroup.getCheckedRadioButtonId();
        btnPackage = (RadioButton) findViewById(selectedId);
        String display_package_selected = btnPackage.getText().toString(); 
        TadGlobals.getTadPackageInstance().setTadPackage(display_package_selected);     

        //Set package price.
        double package_price = 0.00;
        if(display_package_selected == "Bronze"){
            package_price = 150.00;
        }else if(display_package_selected == "Silver"){
            package_price = 225.00;
        }else if(display_package_selected == "Gold"){
            package_price = 375.00;
        }else{
            package_price = 525.00;
        }
        TadGlobals.getPackagePriceInstance().setPackagePrice(package_price);

6个回答

Jave_
Jave_   2013.06.18 14:40
已采纳

很显然,你这个是在初始化做的,获取btn的按钮的值
而你并没有写选中的监听事件

    a.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup radioGroup, int i) {
            //To change body of implemented methods use File | Settings | File Templates.
        }
    });

然后按照楼下的代码区设置

cj6585256
cj6585256   2013.06.18 14:33

swith(selectedId ) {
case R.id.childRadioButton1:
package_price = 150.00;
break;
...依次类推

Kakalapa1986
Kakalapa1986   2013.06.18 14:55

使用string.equals()方法进行比较:

if(display_package_selected.equals("Bronze")) 
{
                package_price = 150.00;
}
else if(display_package_selected.equals("Silver"))
{
                package_price = 225.00;
}
else if(display_package_selected.equals("Gold"))
{
                package_price = 375.00;
}
else
{
                package_price = 525.00;
}
ll_0520
ll_0520   2013.06.18 22:34

这个display_package_selected == "Bronze"不对,String不能这样比较,改为display_package_selected.equals("Bronze"),其它几个类似

aomandeshangxiao
aomandeshangxiao   2013.06.24 20:56

监听事件,要有回调响应。回调函数什么时候执行你的代码,要清楚,另外,建议楼主学会自己打log。

mojieke
mojieke   2013.06.24 17:06

给你的radiogroup设置监听事件,然后采用switch形式,把radiobutton的ID传进去,在相应的case ID中设置你的业务逻辑操作即可

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!