Java Android SeekBar值未正确传递到计算方法
在OnSeekbarchangListener类的OnProgressChanged方法中,我有一个计算小费百分比并将其显示在屏幕上的方法。它似乎只在值0和100之间工作,而不在两者之间工作Java Android SeekBar值未正确传递到计算方法,java,android,Java,Android,在OnSeekbarchangListener类的OnProgressChanged方法中,我有一个计算小费百分比并将其显示在屏幕上的方法。它似乎只在值0和100之间工作,而不在两者之间工作 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { // TODO Auto-generated method stub progre
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);