在 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;
}
}