在 OnSeekBarChangeListener 类的 OnProgressChanged 方法中,我有一个方法可以计算小费百分比并将其显示在屏幕上。它似乎只适用于 0 和 100 之间的值,而不适用于任何介于 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;
}
}
最佳答案
我猜你的问题是整数除法。 int tip
除以100
,将始终返回 0,除非 tip
值为 100,在这种情况下结果为 1
.
尝试定义tip
作为float
相反。-
public void calculate(float tip);
和
tipValue = billVal * (tip / 100.0f);
关于java - Android SeekBar 值未正确传递给计算方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19084943/