我正在做一個(gè)應(yīng)用程序,其中我的 textview 根據(jù)選中的復(fù)選框和單選按鈕立即更新自身。我有 3 個(gè)哈希圖來存儲(chǔ)每個(gè)項(xiàng)目的值: 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);這是我的功能,用于顯示我的復(fù)選框更改時(shí)檢查的項(xiàng)目: 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() 中調(diào)用我的函數(shù): //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); }到目前為止,我只能顯示檢查項(xiàng)目的名稱。我試圖為我的 TextView priceSum 做類似的事情來顯示總和,但它不起作用。我還嘗試將計(jì)算和哈希圖標(biāo)記檢索放在 checkEnoughAndMakeDisabled 函數(shù)中,但它崩潰了。您認(rèn)為獲取哈希圖的值并進(jìn)行計(jì)算的最佳方法是什么?我的嘗試: //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]); } }
添加回答
舉報(bào)
0/150
提交
取消