java - Android SeekBar 值未正确传递给计算方法

标签 java android

在 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/

相关文章:

java - 如何在 H2 中将周数格式化为所需格式

android - Google Maps API v2 在 MapFragment 上绘制部分圆圈

iPhone 测试自动化 - 基准工具?

java - Android onPreExecute方法报奇怪错误

android - 带有按钮的列表项不再可点击

java - Jmeter以重复的方式顺序运行单个线程组

java - hibernate 搜索 : configure Facet for custom FieldBridge

java - Paho MQTT 抛出异常

java - ChangeListener 不会更改 JTextArea 中的字体

android - 如何以编程方式在 Genymotion 上刷新 zip