我正在做一个应用程序,其中我的 textview 根据选中的复选框和单选按钮立即更新自身。我有 3 个哈希图来存储每个项目的值: hashMap.put("chkCheese", 2.50); hashMap.put("chkPep", 3.50); hashMap.put("chkChick", 2.00); hashMap.put("chkBeef", 4.00); hashMap.put("chkBlackOlives", 2.00); hashMap.put("chkPine", 1.00); hashMap.put("chkMushroom", 1.00);这是我的功能,用于显示我的复选框更改时检查的项目: private void checkEnoughAndMakeDisabled (CheckBox checkBoxes []) { int count = 0; for (CheckBox cb:checkBoxes) { cb.setEnabled(true); if (cb.isChecked()) { count++; toppingsSelection.add(cb); } } String text=""; for (CheckBox items: toppingsSelection) { text = text + items.getText().toString() + ", "; } toppings.setText(text); if (count >= 5) { for (CheckBox cb:checkBoxes) { if (!cb.isChecked()) { cb.setEnabled(false); } } } toppingsSelection.clear();}在 onCreate() 中调用我的函数: //Display TOPPINGS text on each action made final CompoundButton.OnCheckedChangeListener chgListener = new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) { checkEnoughAndMakeDisabled(checkBox); } }; for (CheckBox tmpCheckBox: checkBox) { tmpCheckBox.setOnCheckedChangeListener(chgListener); }到目前为止,我只能显示检查项目的名称。我试图为我的 TextView priceSum 做类似的事情来显示总和,但它不起作用。我还尝试将计算和哈希图标记检索放在 checkEnoughAndMakeDisabled 函数中,但它崩溃了。您认为获取哈希图的值并进行计算的最佳方法是什么?我的尝试: //Display PRICE text on each action made price.setOnEditorActionListener(new TextView.OnEditorActionListener() { @Override public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) { double cal=0.0; for (int y=0;y<checkBox.length;y++) { if (checkBox[y].isChecked()) { toppingsSelection.add(checkBox[y]); } }
添加回答
举报
0/150
提交
取消