问题描述
- 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);
解决方案
很显然,你这个是在初始化做的,获取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. } });
然后按照楼下的代码区设置
解决方案二:
swith(selectedId ) {
case R.id.childRadioButton1:
package_price = 150.00;
break;
...依次类推
}
解决方案三:
使用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;}
解决方案四:
这个display_package_selected == ""Bronze""不对,String不能这样比较,改为display_package_selected.equals(""Bronze""),其它几个类似
解决方案五:
监听事件,要有回调响应。回调函数什么时候执行你的代码,要清楚,另外,建议楼主学会自己打log。
解决方案六:
给你的radiogroup设置监听事件,然后采用switch形式,把radiobutton的ID传进去,在相应的case ID中设置你的业务逻辑操作即可
时间: 2024-10-03 00:47:42