我正在學習一個飲料店清單,但目前卡在總額跟加料的關係。
勾選中杯顯示45元時候,當我也勾選一個加料時 價格是+10沒錯,但我取消勾選並沒降低而是沒變。
而當我處於兩個CheckBox勾選狀態時,勾選第三個 就變累加+30 +30....
請問我程式碼在哪一部份需要改善呢...
bottled.setOnClickListener( this );
medium.setOnClickListener( this );
large.setOnClickListener( this );
noSugar.setOnClickListener( this );
lowSugar.setOnClickListener( this );
halfSugar.setOnClickListener( this );
lessSugar.setOnClickListener( this );
Standard.setOnClickListener( this );
rmTemp.setOnClickListener( this );
noIce.setOnClickListener( this );
lowIce.setOnClickListener( this );
lessIce.setOnClickListener( this );
normal.setOnClickListener( this );
Perilla.setOnClickListener(Ingredients);
jelly.setOnClickListener(Ingredients);
pearl.setOnClickListener(Ingredients);
aloe.setOnClickListener(Ingredients);
Coconut.setOnClickListener(Ingredients);
pudding.setOnClickListener(Ingredients);
redbeans.setOnClickListener(Ingredients);
jade.setOnClickListener(Ingredients);
less.setOnClickListener( totalprice );
plus.setOnClickListener( totalprice );
}
private OnClickListener totalprice = new
OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.less: {
if (count > 0) {
count--;
textView.setText( "" + count );
if (medium.isChecked()) {
total_price.setText( Integer.toString( Integer.parseInt( "45" ) * count ) );
counter = Integer.parseInt( total_price.getText().toString() );
bottled.setChecked( false );
} else if (bottled.isChecked()) {
total_price.setText( Integer.toString( Integer.parseInt( "45" ) * count * 0 ) );
count = Integer.parseInt( total_price.getText().toString() );
Toast toast = Toast.makeTex
( Main20Activity.this, " 無瓶裝,請重新選擇!", LENGTH_SHORT );
toast.show();}
else {
total_price.setText( Integer.toString( Integer.parseInt( "80" ) * count ) );
counter = Integer.parseInt( total_price.getText().toString() );
bottled.setChecked( false );
}
}
break;
}
case R.id.plus: {
if (count < max) {
count++;
textView.setText( "" + count );
if (large.isChecked()) {
total_price.setText( Integer.toString( Integer.parseInt( "80" ) * count ) );
counter = Integer.parseInt( total_price.getText().toString() );
bottled.setChecked( false );
} else if (bottled.isChecked()) {
total_price.setText( Integer.toString( Integer.parseInt( "45" ) * count * 0 ) );
count = Integer.parseInt( total_price.getText().toString() );
Toast toast = Toast.makeText
( Main20Activity.this, " 無瓶裝,請重新選擇!", LENGTH_SHORT );
toast.show(); }
else {
total_price.setText( Integer.toString( Integer.parseInt( "45" ) * count ) );
counter = Integer.parseInt( total_price.getText().toString() );
bottled.setChecked( false );
}
}
break;
}
}
}
};
private OnClickListener Ingredients = new
OnClickListener() {
@Override
public void onClick(View v) {
if (Perilla.isChecked()) {
counter = counter + Perilla1;
total_price.setText( counter + " " );
}
else
{total_price.setText( counter+ " " );}
if (jelly.isChecked()) {
counter = counter + jelly1;
total_price.setText( counter + " " );
} else
{total_price.setText(counter+ " " );}
if (pearl.isChecked()) {
counter = counter + pearl1;
total_price.setText( counter + " " );
} else
{ total_price.setText( counter+ " " );}
if (aloe.isChecked()) {
counter = counter + aloe1;
total_price.setText( counter + " " );
} else
{ total_price.setText( counter+ " " );}
if (Coconut.isChecked()) {
counter = counter + Cocount1;
total_price.setText( counter + " " );
} else
{total_price.setText( counter+ " " );}
if (pudding.isChecked()) {
counter = counter + pudding1;
total_price.setText( counter + " " );
} else
{ total_price.setText( counter+ " " );}
if (redbeans.isChecked()) {
counter = counter + redbeans1;
total_price.setText( counter + " " );
} else
{ total_price.setText(counter+ " " );}
if (jade.isChecked()) {
counter = counter + jade1;
total_price.setText( counter + " " );
} else
{ total_price.setText(counter+ " " );}
}
};
我想達成的結果就是當我勾選這個加料是會增加至總額,不勾選時則變為原杯裝價格
因為你看起來只有
當勾選某個材料
總金額就加上該材料的金額
但是你都沒有注意到
當某個材料被取消勾選
總金額就扣除該材料金額
onclick(View v)
應該可以取得被點擊的 view
進而取得被點擊的 view id 以及狀態
然後你的onclick就可以改成類似
.onclick(View v) {
switch (v.getId()) {
case 'redbeans': {
total_price += v.isCheck() ? redbeans.value() : -(redbeans.value())
}
}
}
示意 code
請自行找相關 function 替換
如此一來
你的 code 會相對易讀且簡潔
code
// checkbox15 ~ 22 為加料
// checkbox1 ~ 3 為大小
public void myListener(View v) {
Integer price = 0;
switch (v.getId()) {
case R.id.checkBox: {
price = 50;
break;
}
case R.id.checkBox3: {
price = 40;
break;
}
case R.id.checkBox2: {
price = 30;
break;
}
case R.id.checkBox15: {
price = 10;
break;
}
case R.id.checkBox16: {
price = 10;
break;
}
case R.id.checkBox17: {
price = 10;
break;
}
case R.id.checkBox18: {
price = 10;
break;
}
case R.id.checkBox19: {
price = 10;
break;
}
case R.id.checkBox20: {
price = 10;
break;
}
case R.id.checkBox21: {
price = 10;
break;
}
case R.id.checkBox22: {
price = 10;
break;
}
default: {
Log.d(TAG, "錯誤");
break;
}
}
CheckBox checkbox = (CheckBox) v;
if (checkbox.isChecked()) {
totalPrice += price;
} else {
totalPrice -= price;
}
textTotalPrice.setText("總共 "+totalPrice+"元");
}
將所有的 checkbox 綁定這個 function
就可以針對不同的勾選項目和情況
來變更總價
我還蠻新手的 那個case 後面不用加R.id嗎 我打的時候它要求我需要int
要阿
我只是示意而已
上面的 code 大意就是
用 switch 判別 觸發 event 的 view 是哪個
來決定總金額需要加/減的 金額
為什麼我打v.isCheck 的isCheck顯示
「Cannot resolve method 'isCheck()'」....
private OnClickListener Ingredients = new
OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.Perilla: {
total_price += v.isCheck()
Perilla.value() : -(Perilla.value())
}
}
}
}
我應該沒打錯吧...
就說是示意 code 了XD
這個給你參考
好吧我再回答一下,
看了你對其他人回答的回應,
我覺得你在寫這個APP之前,
應該要先把基礎再加強一下,
剛好我去年鐵人賽寫了這篇 [Andriod] Andriod Studio 從入門到進入狀況,
也許你可以參考看看?