RadioGroup折扣计算异常:0%折扣功能失效求助
咖啡馆菜单计算项目折扣功能异常问题
我正在开发一个咖啡馆菜单计算项目,使用RadioGroup区分0%(现金支付)和10%(信用卡支付)折扣。选择10%折扣的单选按钮时代码运行正常,但选择0%折扣的单选按钮时,计算结果仍与10%折扣一致,功能失效。
原代码
package com.example.fo_liaharsono_proj_cafestekom; import android.os.Bundle; import android.app.Activity; import android.view.View; import android.widget.Button; import android.widget.CheckBox; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.TextView; public class MainActivity extends Activity { TextView edHrgAyamG,edHrgAyamC,edHrgKentang,edHrgSoft,edHrgAir,edSubTotal,edStatus,edBayar; CheckBox cbAyamG,cbAyamC,cbKentang,cbSoft,cbAir; RadioGroup radioGroup1; RadioButton rgTunai,rgCredit; Button btnPesan; private boolean checked; private double dSubTotal; private double dBayar; private double dHrgAyamG; private double dHrgAyamC; private double dHrgKentang; private double dHrgSoft; private double dHrgAir; private double Diskon; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); edHrgAyamG = (TextView)findViewById(R.id.edHrgAyamG); edHrgAyamC = (TextView)findViewById(R.id.edHrgAyamC); edHrgKentang = (TextView)findViewById(R.id.edHrgKentang); edHrgSoft = (TextView)findViewById(R.id.edHrgSoft); edHrgAir = (TextView)findViewById(R.id.edHrgAir); edSubTotal = (TextView)findViewById(R.id.edSubTotal); edStatus = (TextView)findViewById(R.id.edStatus); edBayar = (TextView)findViewById(R.id.edBayar); cbAyamG = (CheckBox)findViewById(R.id.cbAyamG); cbAyamC = (CheckBox)findViewById(R.id.cbAyamC); cbKentang = (CheckBox)findViewById(R.id.cbKentang); cbSoft = (CheckBox)findViewById(R.id.cbSoft); cbAir = (CheckBox)findViewById(R.id.cbAir); radioGroup1 = (RadioGroup)findViewById(R.id.radioGroup1); rgTunai = (RadioButton)findViewById(R.id.rgTunai); rgCredit = (RadioButton)findViewById(R.id.rgCredit); btnPesan = (Button)findViewById(R.id.btnPesan); radioGroup1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup arg0, int arg1) { // TODO Auto-generated method stub if(rgTunai.isChecked()) edStatus.setText("Pembayaran Tunai Diskon 0%"); btnPesan.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub dSubTotal = (dHrgAyamG)+(dHrgAyamC)+(dHrgKentang)+(dHrgSoft)+(dHrgAir); Diskon = (dSubTotal*0)/100; dBayar = (dSubTotal-Diskon)*1000; edBayar.setText("Rp. "+dBayar); } }); if(rgCredit.isChecked()) edStatus.setText("Pembayaran Credit Diskon 10%"); btnPesan.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub dSubTotal = (dHrgAyamG)+(dHrgAyamC)+(dHrgKentang)+(dHrgSoft)+(dHrgAir); Diskon = (dSubTotal*10)/100; dBayar = (dSubTotal-Diskon)*1000; edBayar.setText("Rp. "+dBayar); } }); } }); }; public void KlikCheckBox(View v){ checked = ((CheckBox)v).isChecked(); switch (v.getId()){ case R.id.cbAyamG: if(checked)edHrgAyamG.setText("15.000"); else edHrgAyamG.setText("0"); break; case R.id.cbAyamC: if(checked)edHrgAyamC.setText("12.000"); else edHrgAyamC.setText("0"); break; case R.id.cbKentang: if(checked)edHrgKentang.setText("10.000"); else edHrgKentang.setText("0"); break; case R.id.cbSoft: if(checked)edHrgSoft.setText("5.000"); else edHrgSoft.setText("0"); break; case R.id.cbAir: if(checked)edHrgAir.setText("3.000"); else edHrgAir.setText("0"); break; } dHrgAyamG = Double.parseDouble(edHrgAyamG.getText().toString()); dHrgAyamC = Double.parseDouble(edHrgAyamC.getText().toString()); dHrgKentang = Double.parseDouble(edHrgKentang.getText().toString()); dHrgSoft = Double.parseDouble(edHrgSoft.getText().toString()); dHrgAir = Double.parseDouble(edHrgAir.getText().toString()); dSubTotal = ((dHrgAyamG)+(dHrgAyamC)+(dHrgKentang)+(dHrgSoft)+(dHrgAir))*1000; edSubTotal.setText(""+dSubTotal); } }
问题原因
- 重复设置点击监听器:每次切换RadioButton时,都会给
btnPesan新增一个点击监听器,后面的监听器会覆盖前面的。比如先选信用卡再选现金,最后生效的是信用卡的监听器,导致计算结果始终是10%折扣。 - 依赖TextView文本解析价格,容易出现格式错误;折扣计算逻辑重复,代码冗余。
修复方案
将按钮点击监听器从RadioGroup切换事件中移出,只初始化一次,点击时根据当前选中的RadioButton判断折扣比例;同时优化价格存储逻辑,直接用变量维护价格,避免解析TextView文本。
修复后的代码
package com.example.fo_liaharsono_proj_cafestekom; import android.os.Bundle; import android.app.Activity; import android.view.View; import android.widget.Button; import android.widget.CheckBox; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.TextView; public class MainActivity extends Activity { TextView edSubTotal, edStatus, edBayar; CheckBox cbAyamG, cbAyamC, cbKentang, cbSoft, cbAir; RadioGroup radioGroup1; RadioButton rgTunai, rgCredit; Button btnPesan; // 直接定义商品价格常量,避免依赖TextView private static final double PRICE_AYAM_G = 15.000; private static final double PRICE_AYAM_C = 12.000; private static final double PRICE_KENTANG = 10.000; private static final double PRICE_SOFT = 5.000; private static final double PRICE_AIR = 3.000; private double dHrgAyamG = 0; private double dHrgAyamC = 0; private double dHrgKentang = 0; private double dHrgSoft = 0; private double dHrgAir = 0; private double dSubTotal; private double dBayar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 初始化控件 edSubTotal = findViewById(R.id.edSubTotal); edStatus = findViewById(R.id.edStatus); edBayar = findViewById(R.id.edBayar); cbAyamG = findViewById(R.id.cbAyamG); cbAyamC = findViewById(R.id.cbAyamC); cbKentang = findViewById(R.id.cbKentang); cbSoft = findViewById(R.id.cbSoft); cbAir = findViewById(R.id.cbAir); radioGroup1 = findViewById(R.id.radioGroup1); rgTunai = findViewById(R.id.rgTunai); rgCredit = findViewById(R.id.rgCredit); btnPesan = findViewById(R.id.btnPesan); // RadioGroup切换监听,只更新状态文本 radioGroup1.setOnCheckedChangeListener((arg0, arg1) -> { if (rgTunai.isChecked()) { edStatus.setText("Pembayaran Tunai Diskon 0%"); } else if (rgCredit.isChecked()) { edStatus.setText("Pembayaran Credit Diskon 10%"); } }); // 按钮点击监听只设置一次,点击时判断当前选中的RadioButton btnPesan.setOnClickListener(v -> { calculateSubTotal(); double discountRate = rgTunai.isChecked() ? 0 : 10; dBayar = (dSubTotal - (dSubTotal * discountRate / 100)) * 1000; edBayar.setText("Rp. " + dBayar); }); // 默认选中现金支付 rgTunai.setChecked(true); edStatus.setText("Pembayaran Tunai Diskon 0%"); } // 计算小计 private void calculateSubTotal() { dSubTotal = dHrgAyamG + dHrgAyamC + dHrgKentang + dHrgSoft + dHrgAir; edSubTotal.setText(String.valueOf(dSubTotal * 1000)); } public void KlikCheckBox(View v) { boolean checked = ((CheckBox) v).isChecked(); switch (v.getId()) { case R.id.cbAyamG: dHrgAyamG = checked ? PRICE_AYAM_G : 0; break; case R.id.cbAyamC: dHrgAyamC = checked ? PRICE_AYAM_C : 0; break; case R.id.cbKentang: dHrgKentang = checked ? PRICE_KENTANG : 0; break; case R.id.cbSoft: dHrgSoft = checked ? PRICE_SOFT : 0; break; case R.id.cbAir: dHrgAir = checked ? PRICE_AIR : 0; break; } calculateSubTotal(); } }
内容的提问来源于stack exchange,提问作者Lia Harsono
相关产品推荐
相关产品推荐

