0

OnSeekBarChangeListener クラスの OnProgressChanged メソッドには、チップの割合を計算して画面に表示するメソッドがあります。値 0 から 100 でのみ動作するようで、その間の値では動作しません。

public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                // TODO Auto-generated method stub
                progress = ((int)Math.round(progress/5 ))*5;
                seekBar.setProgress(progress);
                String tipValStr = String.valueOf(progress);
                tipVal = progress;
                tvTipVal.setText(tipValStr);
                //Toast.makeText(TipCalc.this, "Seekbar Value : " + progress, Toast.LENGTH_SHORT).show();
                calculate(progress);
            }

public void calculate(int tip){
        try{
        DecimalFormat df = new DecimalFormat("#.00");
        billVal = Double.parseDouble(etBillVal.getText().toString());
        //tipVal = sbTipVal.getProgress();
        tipValue = billVal * (tip/100);
        billTotal = billVal + tipValue;
        tvBillVal.setText("$"+df.format(billVal));
        tvTipValue.setText("+ $" + df.format(tipValue));
        tvBillTotal.setText("$"+df.format(billTotal));
        Toast.makeText(TipCalc.this, "$"+df.format(billTotal), Toast.LENGTH_SHORT).show();
        }
        catch(Exception e){
            //billVal = 0;
        }
    }
4

1 に答える 1

1

問題は整数除算だと思います。intを でtip割った値は、value が 100 で100ない限り、常に 0 を返します。tip値が 100 の場合、結果は になり1ます。

代わりに として定義tipしてみてくださいfloat。-

public void calculate(float tip);

tipValue = billVal * (tip / 100.0f);
于 2013-09-30T00:45:23.807 に答える